{"id":124514,"date":"2026-11-19T03:08:36","date_gmt":"2026-11-18T19:08:36","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/124514.html"},"modified":"2026-11-19T03:08:36","modified_gmt":"2026-11-18T19:08:36","slug":"mybatis-plus-%e5%88%86%e9%a1%b5%e7%9a%84-count-%e6%9f%a5%e8%af%a2%e8%a2%ab%e9%99%8d%e7%ba%a7%e4%ba%86%ef%bc%9a%e9%82%a3%e8%a1%8c-optimize-this-sql%ef%bc%8c%e4%b8%8d%e6%98%af%e8%ae%a9%e4%bd%a0%e5%85%b3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/124514.html","title":{"rendered":"MyBatis-Plus \u5206\u9875\u7684 count \u67e5\u8be2\u88ab\u964d\u7ea7\u4e86\uff1a\u90a3\u884c optimize this sql\uff0c\u4e0d\u662f\u8ba9\u4f60\u5173\u65e5\u5fd7"},"content":{"rendered":"<p>        \/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/<br \/>\n        #content_views .toc,<br \/>\n        \/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/<br \/>\n        #content_views.markdown_views &gt; p:empty:has(+ .toc),<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty,<br \/>\n        \/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/<br \/>\n        #content_views.htmledit_views #main-toc,<br \/>\n        #content_views.htmledit_views #hr-toc,<br \/>\n        #content_views.htmledit_views p[id*=\\&#8221;-toc\\&#8221;] {<br \/>\n          display: none !important;<br \/>\n        }<br \/>\n        \/* \u76ee\u5f55\u53bb\u6389\u540e\uff0c\u7d27\u8ddf\u7684\u9996\u4e2a\u6807\u9898\u4e0d\u518d\u591a\u51fa\u4e00\u5757\u4e0a\u8fb9\u8ddd *\/<br \/>\n        #content_views.markdown_views &gt; .toc + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + h4,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h4 {<br \/>\n          margin-top: 0 !important;<br \/>\n        }<\/p>\n<p>\u5206\u9875\u63a5\u53e3\u7a81\u7136\u6162\u4e0b\u6765&#xff0c;\u65e5\u5fd7\u91cc\u5374\u6ca1\u6709\u4e00\u6761\u660e\u663e\u7684\u6162 SQL&#xff0c;\u8fd9\u79cd\u60c5\u51b5\u633a\u5bb9\u6613\u8ba9\u4eba\u5148\u53bb\u6000\u7591\u6570\u636e\u5e93\u6216\u8005\u8fde\u63a5\u6c60\u3002<\/p>\n<p>\u6211\u8fd9\u6b21\u9047\u5230\u7684\u73b0\u8c61\u662f&#xff1a;\u65e5\u5fd7\u91cc\u51fa\u73b0\u4e00\u884c optimize this sql to a count sql has exception&#xff0c;\u540e\u9762\u8ddf\u7740\u4e00\u6761\u5f88\u957f\u7684 SELECT&#xff0c;\u8fd8\u6709 JSqlParser \u7684 ParseException\u3002\u5206\u9875\u7ed3\u679c\u662f\u5bf9\u7684&#xff0c;\u603b\u6570\u4e5f\u5bf9&#xff0c;\u7b2c\u4e00\u53cd\u5e94\u786e\u5b9e\u662f\u60f3\u628a\u8fd9\u6761 warning \u5173\u6389\u3002<\/p>\n<p>\u4f46\u540e\u6765\u6709\u4e2a\u5217\u8868\u63a5\u53e3\u6162\u5230\u63a5\u8fd1 3 \u79d2\u3002\u6211\u7ffb\u4e86 MyBatis \u7684 Preparing \u65e5\u5fd7&#xff0c;\u53d1\u73b0\u6162\u7684\u4e0d\u662f\u5e26 LIMIT \u7684\u5206\u9875 SQL&#xff0c;\u800c\u662f\u5b83\u524d\u9762\u90a3\u6761 count\u3002<\/p>\n<p>\u6211\u5728\u672c\u673a\u642d\u4e86\u4e00\u5f20 200 \u4e07\u884c\u7684\u8ba2\u5355\u8868\u548c\u4e00\u5f20 400 \u4e07\u884c\u7684\u660e\u7ec6\u8868&#xff0c;\u628a\u51e0\u79cd\u5e38\u89c1\u5199\u6cd5\u5206\u522b\u8dd1\u4e86\u4e00\u904d\u3002\u7ed3\u679c\u6709\u70b9\u51fa\u4e4e\u610f\u6599&#xff1a;\u6709 warning \u7684\u90a3\u79cd\u5199\u6cd5&#xff0c;count \u5927\u7ea6 340ms&#xff1b;\u53e6\u4e00\u79cd\u6ca1\u6709\u4efb\u4f55 warning \u7684\u5199\u6cd5&#xff0c;count \u5374\u8dd1\u4e86 2.1 \u79d2\u3002<\/p>\n<p>\u6cbf\u7740\u8fd9\u4e2a\u95ee\u9898\u6df1\u5165\u63a2\u7a76\u4e86\u4e0b&#xff0c;MyBatis-Plus \u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u6539\u5199 count&#xff0c;\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u76f4\u63a5\u964d\u7ea7&#xff0c;\u4ee5\u53ca\u9047\u5230\u4ee5\u540e\u53ef\u4ee5\u600e\u4e48\u5904\u7406\u3002<\/p>\n<h3>\u4e00\u3001\u5206\u9875\u65f6&#xff0c;MyBatis-Plus \u4f1a\u5148\u5904\u7406 count<\/h3>\n<p>\u4f7f\u7528 MyBatis-Plus \u5206\u9875\u63d2\u4ef6\u65f6&#xff0c;\u5982\u679c\u5f00\u542f\u4e86\u603b\u6570\u67e5\u8be2&#xff0c;\u4e00\u6b21\u5206\u9875\u8bf7\u6c42\u901a\u5e38\u4f1a\u6267\u884c\u4e24\u6761 SQL&#xff1a;\u5148\u67e5\u603b\u6570&#xff0c;\u518d\u67e5\u5f53\u524d\u9875\u6570\u636e\u3002<\/p>\n<p>\u65e5\u5fd7\u91cc\u4e00\u822c\u80fd\u770b\u5230\u4e24\u6761 Preparing&#xff1a;\u7b2c\u4e00\u6761\u662f count&#xff0c;\u7b2c\u4e8c\u6761\u624d\u662f\u5e26 LIMIT \u7684\u5206\u9875\u67e5\u8be2\u3002<\/p>\n<p>&#061;&#061;&gt;  Preparing: SELECT COUNT(*) FROM (SELECT id, user_id, biz_code &#8230; ) TOTAL<br \/>\n&#061;&#061;&gt; Parameters: 1(Integer), biz01(String)<br \/>\n&#061;&#061;&gt;  Preparing: SELECT id, user_id, biz_code &#8230; LIMIT ?<\/p>\n<p>\u8fd9\u91cc\u6709\u4e2a\u5bb9\u6613\u5ffd\u7565\u7684\u5730\u65b9&#xff0c;\u7b2c\u4e00\u6761 count \u5f80\u5f80\u4e0d\u662f mapper \u91cc\u624b\u5199\u7684 SQL&#xff0c;\u800c\u662f MyBatis-Plus \u5206\u9875\u63d2\u4ef6\u5728\u8fd0\u884c\u65f6\u751f\u6210\u7684\u3002<\/p>\n<p>\u5728 MyBatis-Plus 3.5.16 \u4e2d&#xff0c;\u6838\u5fc3\u903b\u8f91\u5728 PaginationInnerInterceptor#autoCountSql\u3002\u5b83\u4f1a\u5c1d\u8bd5\u89e3\u6790\u539f\u59cb SELECT&#xff0c;\u7136\u540e\u6839\u636e SQL \u7ed3\u6784\u6539\u5199 count&#xff1a;\u4f8b\u5982\u628a\u67e5\u8be2\u5217\u66ff\u6362\u6210 COUNT(*)&#xff0c;\u53bb\u6389\u4e0d\u5f71\u54cd\u603b\u6570\u7684 ORDER BY&#xff0c;\u518d\u68c0\u67e5 DISTINCT\u3001GROUP BY\u3001JOIN \u7b49\u60c5\u51b5\u3002<\/p>\n<p>\u53ef\u4ee5\u5148\u628a\u5b83\u7406\u89e3\u6210\u4e24\u6761\u8def&#xff1a;<\/p>\n<ul>\n<li>\u80fd\u5b89\u5168\u6539\u5199&#xff1a;\u751f\u6210\u66f4\u7cbe\u7b80\u7684 count SQL\u3002<\/li>\n<li>\u4e0d\u80fd\u6539\u5199\u6216\u89e3\u6790\u5931\u8d25&#xff1a;\u9000\u56de\u5230\u5305\u88c5\u539f SQL \u7684\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u5173\u952e\u903b\u8f91\u5927\u81f4\u5982\u4e0b&#xff0c;\u7701\u7565\u4e86\u4e2d\u95f4\u7684 SQL \u7ed3\u6784\u5224\u65ad&#xff1a;<\/p>\n<p><span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">Select<\/span> select <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Select<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token class-name\">JsqlParserGlobal<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">parse<\/span><span class=\"token punctuation\">(<\/span>sql<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u6839\u636e SQL \u7ed3\u6784\u5224\u65ad\u662f\u5426\u80fd\u5b89\u5168\u4f18\u5316<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u7701\u7565 DISTINCT\u3001GROUP BY\u3001JOIN\u3001ORDER BY \u7b49\u5904\u7406<\/span><br \/>\n    plainSelect<span class=\"token punctuation\">.<\/span><span class=\"token function\">setSelectItems<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant\">COUNT_SELECT_ITEM<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> select<span class=\"token punctuation\">.<\/span><span class=\"token function\">toString<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">catch<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">JSQLParserException<\/span> e<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    logger<span class=\"token punctuation\">.<\/span><span class=\"token function\">warn<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;optimize this sql to a count sql has exception, sql:{}&#034;<\/span><span class=\"token punctuation\">,<\/span> sql<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">catch<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Exception<\/span> e<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    logger<span class=\"token punctuation\">.<\/span><span class=\"token function\">warn<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;optimize this sql to a count sql has error, sql:{}&#034;<\/span><span class=\"token punctuation\">,<\/span> sql<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">return<\/span> <span class=\"token function\">lowLevelCountSql<\/span><span class=\"token punctuation\">(<\/span>sql<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u964d\u7ea7\u65b9\u6cd5\u672c\u8eab\u5f88\u7b80\u5355&#xff1a;<\/p>\n<p><span class=\"token keyword\">protected<\/span> <span class=\"token class-name\">String<\/span> <span class=\"token function\">lowLevelCountSql<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span> originalSql<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token class-name\">SqlParserUtils<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">getOriginalCountSql<\/span><span class=\"token punctuation\">(<\/span>originalSql<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token class-name\">String<\/span> <span class=\"token function\">getOriginalCountSql<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span> originalSql<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token class-name\">String<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">format<\/span><span class=\"token punctuation\">(<\/span><br \/>\n        <span class=\"token string\">&#034;SELECT COUNT(*) FROM (%s) TOTAL&#034;<\/span><span class=\"token punctuation\">,<\/span> originalSql<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u539f\u672c\u60f3\u751f\u6210\u4e00\u6761\u66f4\u7cbe\u7b80\u7684 count&#xff0c;\u7ed3\u679c\u6ca1\u6709\u6539\u5199\u6210\u529f&#xff0c;\u5c31\u628a\u539f\u67e5\u8be2\u5305\u5728\u5b50\u67e5\u8be2\u5916\u9762&#xff0c;\u518d\u7edf\u8ba1\u5b50\u67e5\u8be2\u7684\u884c\u6570\u3002<\/p>\n<p>\u4ee3\u4ef7\u5dee\u522b\u5c31\u5728\u8fd9\u91cc&#xff0c;\u6539\u5199\u6210\u529f\u65f6&#xff0c;count \u53ea\u9700\u8981\u626b\u7d22\u5f15\u3001\u6570\u884c\u6570\u3002\u6539\u5199\u5931\u8d25\u65f6&#xff0c;\u6570\u636e\u5e93\u8981\u5148\u628a\u539f\u67e5\u8be2\u7684\u7ed3\u679c\u8dd1\u51fa\u6765\u3001\u7269\u5316\u6210\u4e00\u4e2a\u6d3e\u751f\u8868&#xff0c;\u518d\u6570\u8fd9\u5f20\u8868\u6709\u591a\u5c11\u884c\u3002\u539f\u67e5\u8be2\u5982\u679c\u5e26\u4e86 ORDER BY\u3001\u5f88\u5bbd\u7684 SELECT \u5217\u8868\u6216\u8005 JOIN&#xff0c;\u8fd9\u4e9b\u4ee3\u4ef7\u5c31\u5168\u90e8\u843d\u5230 count \u4e0a\u3002<\/p>\n<p>\u4e24\u4e2a catch \u90fd\u4e0d\u4f1a\u8ba9\u5206\u9875\u8bf7\u6c42\u76f4\u63a5\u5931\u8d25&#xff0c;\u6253\u5370 warning \u540e\u7ee7\u7eed\u8d70\u964d\u7ea7\u3002\u6240\u4ee5\u770b\u5230\u8fd9\u884c\u65e5\u5fd7&#xff0c;\u5206\u9875\u7ed3\u679c\u4e0d\u4e00\u5b9a\u6709\u95ee\u9898&#xff0c;\u4f46 count \u7684\u6267\u884c\u65b9\u5f0f\u53ef\u80fd\u5df2\u7ecf\u53d8\u4e86\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a\u5bb9\u6613\u8bef\u7528\u7684\u5f00\u5173&#xff0c;page.setOptimizeCountSql(false)\u3002\u8fd9\u4e2a\u9009\u9879\u9ed8\u8ba4\u662f true&#xff1b;\u624b\u52a8\u5173\u95ed\u540e&#xff0c;\u4f1a\u76f4\u63a5\u4f7f\u7528\u4f4e\u7ea7 count \u5305\u88c5\u903b\u8f91&#xff0c;\u4e0d\u9700\u8981\u5148\u89e3\u6790&#xff0c;\u81ea\u7136\u4e5f\u4e0d\u4f1a\u51fa\u73b0\u89e3\u6790\u5931\u8d25\u7684 warning\u3002\u5b83\u4e0d\u662f&#034;\u8ba9\u65e5\u5fd7\u5e72\u51c0&#034;\u7684\u5f00\u5173&#xff0c;\u5173\u95ed\u540e\u53cd\u800c\u53ef\u80fd\u8ba9\u6bcf\u6b21 count \u90fd\u8d70\u5b50\u67e5\u8be2\u3002<\/p>\n<p>\u63a5\u53e3\u53d8\u6162\u65f6&#xff0c;\u5148\u628a count \u548c page \u4e24\u6761 SQL \u5206\u5f00\u770b\u3002\u4e0d\u8981\u628a\u5b83\u4eec\u7684\u8017\u65f6\u6df7\u5728\u4e00\u8d77\u3002<\/p>\n<h3>\u4e8c\u3001\u4e00\u4e2a BINARY&#xff0c;\u8ba9 count \u4ece 7ms \u53d8\u6210 340ms<\/h3>\n<p>\u5148\u770b\u4e00\u7ec4\u6700\u76f4\u89c2\u7684\u5bf9\u6bd4\u3002\u4e24\u6761 SQL \u53ea\u6709\u4e00\u4e2a\u5730\u65b9\u4e0d\u540c\u3002<\/p>\n<p>\u666e\u901a\u7b49\u503c\u67e5\u8be2&#xff1a;<\/p>\n<p><span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token comment\">#{status}<\/span><br \/>\n  <span class=\"token operator\">AND<\/span> biz_code <span class=\"token operator\">&#061;<\/span> <span class=\"token comment\">#{biz}<\/span><\/p>\n<p>\u7ed9\u5217\u52a0\u4e0a BINARY&#xff0c;\u52a0BINARY \u5b83\u901a\u5e38\u51fa\u73b0\u5728\u8fd9\u6837\u7684\u573a\u666f&#xff1a;biz_code \u8fd9\u7c7b\u7f16\u7801\u7531\u8fd0\u8425\u540e\u53f0\u586b\u5199&#xff0c;\u6216\u8005\u4ece\u7b2c\u4e09\u65b9\u7cfb\u7edf\u540c\u6b65&#xff0c;\u503c\u4e0d\u662f\u7cfb\u7edf\u81ea\u5df1\u751f\u6210\u7684&#xff0c;\u5927\u5c0f\u5199\u5e76\u4e0d\u7edf\u4e00&#xff0c;\u800c\u5217\u5efa\u8868\u65f6\u7528\u4e86\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u6392\u5e8f\u89c4\u5219&#xff0c;\u4e8e\u662f WHERE biz_code &#061; \u2018biz01\u2019 \u4f1a\u628a \u2018BIZ01\u2019 \u4e5f\u67e5\u51fa\u6765\u3002\u6539\u5217\u7684\u6392\u5e8f\u89c4\u5219\u4f1a\u5f71\u54cd\u6574\u5f20\u8868&#xff0c;\u5199 COLLATE utf8mb4_bin \u53c8\u5570\u55e6&#xff0c;\u6700\u7701\u4e8b\u7684\u529e\u6cd5\u5c31\u662f\u5728 SQL \u91cc\u52a0\u4e00\u4e2a BINARY&#xff0c;\u5f3a\u5236\u6309\u5b57\u8282\u6bd4\u8f83\u3002\u4ee3\u4ef7\u662f\u8fd9\u4e00\u5217\u5728\u67e5\u8be2\u91cc\u4e0d\u518d\u8d70\u666e\u901a\u7d22\u5f15&#xff1a;<\/p>\n<p><span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token comment\">#{status}<\/span><br \/>\n  <span class=\"token operator\">AND<\/span> <span class=\"token keyword\">BINARY<\/span> biz_code <span class=\"token operator\">&#061;<\/span> <span class=\"token comment\">#{biz}<\/span><\/p>\n<p>\u7b2c\u4e00\u6761 SQL \u7684 count \u53ef\u4ee5\u88ab\u6539\u5199\u6210&#xff1a;<\/p>\n<p><span class=\"token keyword\">SELECT<\/span> <span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> total<br \/>\n<span class=\"token keyword\">FROM<\/span> orders<br \/>\n<span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> ?<br \/>\n  <span class=\"token operator\">AND<\/span> biz_code <span class=\"token operator\">&#061;<\/span> ?<\/p>\n<p>\u7b2c\u4e8c\u6761\u6ca1\u6709\u6539\u5199\u6210\u529f&#xff0c;\u5b9e\u9645\u751f\u6210\u7684\u662f\u5916\u5c42\u5305\u88c5&#xff1a;<\/p>\n<p><span class=\"token keyword\">SELECT<\/span> <span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> <span class=\"token punctuation\">(<\/span><br \/>\n    <span class=\"token keyword\">SELECT<\/span> id<span class=\"token punctuation\">,<\/span> user_id<span class=\"token punctuation\">,<\/span> biz_code<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">status<\/span><span class=\"token punctuation\">,<\/span> amount<span class=\"token punctuation\">,<\/span> created_at<br \/>\n    <span class=\"token keyword\">FROM<\/span> orders<br \/>\n    <span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> ?<br \/>\n      <span class=\"token operator\">AND<\/span> <span class=\"token keyword\">BINARY<\/span> biz_code <span class=\"token operator\">&#061;<\/span> ?<br \/>\n    <span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> created_at <span class=\"token keyword\">DESC<\/span><br \/>\n<span class=\"token punctuation\">)<\/span> TOTAL<\/p>\n<p>\u8868\u4e0a\u6709\u4e09\u4e2a\u7d22\u5f15&#xff1a;(biz_code, status)\u3001(user_id, status, created_at) \u548c (created_at)&#xff0c;\u6ca1\u6709\u4e13\u95e8\u4e3a BINARY \u90a3\u6761\u67e5\u8be2\u5efa\u7acb\u7684 (status, biz_code) \u7d22\u5f15\u3002<\/p>\n<p>\u4e0b\u9762\u63d0\u5230\u7684\u8017\u65f6\u90fd\u662f MySQL \u670d\u52a1\u7aef\u8bb0\u5f55\u7684\u8bed\u53e5\u6267\u884c\u65f6\u95f4&#xff0c;\u4e0d\u5305\u542b HikariCP \u83b7\u53d6\u8fde\u63a5\u7684\u7b49\u5f85&#xff0c;\u4e5f\u4e0d\u662f\u5b8c\u6574\u7684 JDBC \u5f80\u8fd4\u65f6\u95f4\u3002\u6bcf\u7ec4\u8dd1 5 \u6b21&#xff0c;\u53d6\u4e2d\u4f4d\u6570\u3002<\/p>\n<p>\u666e\u901a\u7b49\u503c\u67e5\u8be2\u7684\u4e24\u4e2a\u6761\u4ef6\u90fd\u662f\u7b49\u503c\u6761\u4ef6&#xff0c;(biz_code, status) \u8fd9\u4e24\u5217\u90fd\u80fd\u7528\u4e0a&#xff1b;where \u91cc\u4e24\u4e2a\u6761\u4ef6\u7684\u5148\u540e\u987a\u5e8f\u4e0d\u5f71\u54cd\u80fd\u4e0d\u80fd\u7528\u4e0a\u7d22\u5f15\u3002\u5b83\u7684 count \u4e2d\u4f4d\u6570\u7ea6 7ms\u3002<\/p>\n<p>\u52a0\u4e0a BINARY \u4e4b\u540e&#xff0c;\u6bd4\u8f83\u8868\u8fbe\u5f0f\u53d8\u4e86\u3002\u539f\u7d22\u5f15\u6309\u5217\u672c\u8eab\u7684\u6392\u5e8f\u89c4\u5219\u7ec4\u7ec7&#xff0c;\u4e0d\u80fd\u518d\u6309\u539f\u6765\u7684\u666e\u901a\u7b49\u503c\u6761\u4ef6\u76f4\u63a5\u5b9a\u4f4d\u3002\u672c\u6b21\u6267\u884c\u8ba1\u5212\u9000\u56de\u5230 Table scan on orders&#xff0c;\u626b\u63cf\u7ea6 200 \u4e07\u884c&#xff0c;\u8fc7\u6ee4\u540e\u5269\u4e0b 33,348 \u884c&#xff0c;count \u4e2d\u4f4d\u6570\u7ea6 340ms\u3002<\/p>\n<p>\u8fd9\u91cc\u7684 EXPLAIN \u4f30\u7b97\u884c\u6570\u548c\u5b9e\u9645\u884c\u6570\u6709\u5dee\u522b&#xff1a;\u666e\u901a\u7b49\u503c\u67e5\u8be2\u90a3\u6761\u4f30\u7b97 65,624 \u884c&#xff0c;\u5b9e\u9645\u547d\u4e2d 33,348 \u884c\u3002\u6570\u636e\u521a\u704c\u5b8c\u3001\u7edf\u8ba1\u4fe1\u606f\u8fd8\u6ca1\u66f4\u65b0\u65f6&#xff0c;\u4f30\u7b97\u53ef\u80fd\u4e0d\u51c6&#xff1b;\u8981\u770b\u5b9e\u9645\u626b\u63cf\u91cf&#xff0c;\u5e94\u8be5\u7ed3\u5408 EXPLAIN ANALYZE \u7684 actual rows\u3002<\/p>\n<p>\u8fd9\u7ec4\u5dee\u5f02\u5176\u5b9e\u6709\u4e24\u4e2a\u539f\u56e0&#xff0c;\u6700\u597d\u5206\u5f00\u7406\u89e3&#xff1a;<\/p>\n<li>MyBatis-Plus \u6ca1\u6709\u6210\u529f\u6539\u5199 count\u3002 JSqlParser 5.2 \u89e3\u6790\u8fd9\u6761 SQL \u65f6&#xff0c;\u5728 BINARY biz_code \u9644\u8fd1\u62a5\u9519&#xff0c;\u4e8e\u662f\u6253\u5370 warning \u5e76\u964d\u7ea7\u3002<\/li>\n<li>MySQL \u6ca1\u6cd5\u6309\u539f\u7d22\u5f15\u6761\u4ef6\u76f4\u63a5\u5b9a\u4f4d\u3002 BINARY \u6539\u53d8\u4e86\u6bd4\u8f83\u65b9\u5f0f&#xff0c;\u6267\u884c\u8ba1\u5212\u4ece\u7d22\u5f15\u8986\u76d6\u626b\u63cf\u9000\u56de\u5230\u5168\u8868\u626b\u63cf\u3002<\/li>\n<p>warning \u91cc\u80fd\u770b\u5230\u5177\u4f53 token&#xff1a;<\/p>\n<p>Encountered unexpected token: &#034;biz_code&#034; &lt;S_IDENTIFIER&gt;<\/p>\n<p>\u6240\u4ee5\u8fd9\u6761 warning \u8fd8\u662f\u6709\u7528\u7684&#xff1a;\u5b83\u80fd\u544a\u8bc9\u4f60\u54ea\u6761 SQL \u6ca1\u6709\u88ab\u89e3\u6790\u6210\u529f\u3002\u4f46\u8ba9\u8fd9\u6b21 count \u53d8\u6162\u7684&#xff0c;\u662f\u964d\u7ea7\u540e\u7684 SQL&#xff0c;\u4ee5\u53ca\u968f\u4e4b\u6539\u53d8\u7684\u6267\u884c\u8ba1\u5212\u3002<\/p>\n<p>\u5982\u679c\u60f3\u5355\u72ec\u786e\u8ba4 JSqlParser \u80fd\u4e0d\u80fd\u89e3\u6790\u67d0\u6761 SQL&#xff0c;\u4e0d\u9700\u8981\u542f\u52a8 Spring&#xff0c;\u5199\u4e2a\u7b80\u5355\u7684 main \u5c31\u53ef\u4ee5&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">String<\/span> sql <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;SELECT id FROM orders &#034;<\/span><br \/>\n            <span class=\"token operator\">&#043;<\/span> <span class=\"token string\">&#034;WHERE BINARY biz_code &#061; &#039;biz01&#039;&#034;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token class-name\">JsqlParserGlobal<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">parse<\/span><span class=\"token punctuation\">(<\/span>sql<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;parsed&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">catch<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Throwable<\/span> e<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>e<span class=\"token punctuation\">.<\/span><span class=\"token function\">getClass<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">getName<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>e<span class=\"token punctuation\">.<\/span><span class=\"token function\">getMessage<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u6211\u4e5f\u628a\u5341\u51e0\u79cd\u5e38\u89c1\u5199\u6cd5\u8dd1\u4e86\u4e00\u904d&#xff0c;\u5305\u62ec -&gt;&gt;\u3001GROUP_CONCAT &#8230; SEPARATOR\u3001MATCH &#8230; AGAINST\u3001FORCE INDEX\u3001GROUP BY &#8230; WITH ROLLUP\u3001\u7a97\u53e3\u51fd\u6570\u3001DATE_SUB \u548c\u4f18\u5316\u5668 hint\u3002\u5728\u8fd9\u7ec4\u7248\u672c\u548c\u6d4b\u8bd5 SQL \u91cc&#xff0c;\u53ea\u6709 BINARY \u89e6\u53d1\u4e86\u5f02\u5e38\u3002\u4e0d\u662f MySQL \u65b9\u8a00\u4e00\u5b9a\u4f1a\u70b8&#xff0c;\u662f\u8fd9\u4e00\u79cd\u5199\u6cd5\u8e29\u5230\u4e86\u89e3\u6790\u5668\u7684\u7a7a\u4f4d\u3002<\/p>\n<h3>\u4e09\u3001\u6ca1\u6709 warning&#xff0c;count \u4e5f\u53ef\u80fd\u5df2\u7ecf\u964d\u7ea7<\/h3>\n<p>\u524d\u9762 BINARY \u7684\u60c5\u51b5\u6bd4\u8f83\u5bb9\u6613\u53d1\u73b0&#xff1a;\u89e3\u6790\u5931\u8d25\u4f1a\u6253\u5370 warning\u3002<\/p>\n<p>\u4f46\u8fd8\u6709\u4e00\u4e9b SQL&#xff0c;MyBatis-Plus \u4f1a\u6839\u636e SQL \u7ed3\u6784\u4e3b\u52a8\u653e\u5f03\u6539\u5199&#xff0c;\u76f4\u63a5\u8d70\u4f4e\u7ea7 count\u3002\u5b83\u8ba4\u4e3a\u76f4\u63a5\u66ff\u6362\u6210 COUNT(*) \u53ef\u80fd\u6539\u53d8\u539f\u67e5\u8be2\u7684\u7edf\u8ba1\u7ed3\u679c&#xff0c;\u6240\u4ee5\u4e0d\u4f1a\u6253\u5370\u89e3\u6790\u5f02\u5e38\u7684 warning\u3002<\/p>\n<p>\u5e38\u89c1\u60c5\u51b5\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>DISTINCT&#xff1a;\u53bb\u91cd\u4f1a\u6539\u53d8\u7ed3\u679c\u884c\u6570\u3002<\/li>\n<li>GROUP BY&#xff1a;count \u9700\u8981\u7edf\u8ba1\u5206\u7ec4\u540e\u7684\u884c\u6570&#xff0c;\u4e0d\u662f\u539f\u8868\u8bb0\u5f55\u6570\u3002<\/li>\n<li>UNION \/ UNION ALL&#xff1a;\u9700\u8981\u8003\u8651\u5408\u5e76\u540e\u7684\u7ed3\u679c\u96c6\u3002<\/li>\n<li>select \u5217\u8868\u4e2d\u5b58\u5728\u53c2\u6570\u5360\u4f4d\u7b26&#xff1a;\u63d2\u4ef6\u65e0\u6cd5\u7b80\u5355\u786e\u8ba4\u66ff\u6362\u540e\u662f\u5426\u4ecd\u7136\u4fdd\u6301\u539f\u6765\u7684\u884c\u6570\u8bed\u4e49\u3002<\/li>\n<\/ul>\n<p>\u770b\u4e24\u4e2a\u4f8b\u5b50\u3002<\/p>\n<p>GROUP BY&#xff1a;count \u6570\u7684\u662f\u5206\u7ec4\u6570\u3002<\/p>\n<p><span class=\"token keyword\">SELECT<\/span> user_id<span class=\"token punctuation\">,<\/span> <span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> cnt<span class=\"token punctuation\">,<\/span> <span class=\"token function\">SUM<\/span><span class=\"token punctuation\">(<\/span>amount<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> total<br \/>\n<span class=\"token keyword\">FROM<\/span> orders<br \/>\n<span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token comment\">#{status}<\/span><br \/>\n<span class=\"token keyword\">GROUP<\/span> <span class=\"token keyword\">BY<\/span> user_id<br \/>\n<span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> total <span class=\"token keyword\">DESC<\/span><\/p>\n<p>status &#061; 1 \u547d\u4e2d\u7ea6 33 \u4e07\u884c&#xff0c;\u6309 user_id \u5206\u6210 11,716 \u7ec4\u3002\u5206\u9875\u603b\u6570\u5e94\u8be5\u662f\u5206\u7ec4\u540e\u7684 11,716&#xff0c;\u800c\u4e0d\u662f\u539f\u59cb\u7684 33 \u4e07\u884c\u3002\u6570\u636e\u5e93\u9700\u8981\u5b8c\u6210\u5206\u7ec4&#xff0c;\u518d\u7edf\u8ba1\u5206\u7ec4\u7ed3\u679c&#xff0c;\u6240\u4ee5\u8fd9\u6761 count \u7ea6 2.1 \u79d2\u3002<\/p>\n<p>UNION ALL&#xff1a;count \u6570\u7684\u662f\u5408\u5e76\u540e\u7684\u884c\u6570\u3002<\/p>\n<p><span class=\"token keyword\">SELECT<\/span> id<span class=\"token punctuation\">,<\/span> amount <span class=\"token keyword\">FROM<\/span> orders <span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n<span class=\"token keyword\">UNION<\/span> <span class=\"token keyword\">ALL<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> id<span class=\"token punctuation\">,<\/span> amount <span class=\"token keyword\">FROM<\/span> orders <span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">2<\/span><\/p>\n<p>\u672c\u6b21\u4e24\u8fb9\u5404\u6709\u7ea6 33 \u4e07\u884c&#xff0c;\u5408\u5e76\u540e\u7ea6 66 \u4e07\u884c\u3002count \u9700\u8981\u7edf\u8ba1\u5408\u5e76\u7ed3\u679c&#xff0c;\u5b9e\u6d4b\u7ea6 850ms\u3002<\/p>\n<p>\u8fd9\u91cc\u8981\u6ce8\u610f&#xff1a;GROUP BY \u90a3\u6761\u7684 2.1 \u79d2\u548c\u666e\u901a\u7b49\u503c\u67e5\u8be2\u7684 7ms \u4e0d\u662f\u540c\u4e00\u79cd\u6570\u636e\u91cf\u7ea7\u3002\u666e\u901a\u7b49\u503c\u67e5\u8be2\u540c\u65f6\u4f7f\u7528 status \u548c biz_code&#xff0c;\u53ea\u547d\u4e2d 33,348 \u884c&#xff1b;GROUP BY \u90a3\u6761\u53ea\u6709 status \u6761\u4ef6&#xff0c;\u53c2\u4e0e\u5206\u7ec4\u7684\u6709\u7ea6 33 \u4e07\u884c\u3002\u4e0d\u80fd\u628a\u8fd9\u4e24\u4e2a\u8017\u65f6\u7b80\u5355\u7406\u89e3\u6210 SQL \u6539\u5199\u672c\u8eab\u5e26\u6765\u7684\u5dee\u5f02\u3002<\/p>\n<p>\u6240\u4ee5\u6392\u67e5\u65f6\u8981\u533a\u5206\u4e24\u79cd\u60c5\u51b5&#xff1a;<\/p>\n<ul>\n<li>\u89e3\u6790\u5931\u8d25\u5bfc\u81f4\u7684\u964d\u7ea7&#xff1a;\u901a\u5e38\u4f1a\u6253\u5370 optimize this sql &#8230; has exception\u3002<\/li>\n<li>\u7ed3\u6784\u5224\u65ad\u540e\u4e3b\u52a8\u964d\u7ea7&#xff1a;\u53ef\u80fd\u6ca1\u6709 warning&#xff0c;\u4f46\u6700\u7ec8\u540c\u6837\u4f1a\u751f\u6210\u5916\u5c42 count \u5b50\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u5206\u9875\u6162\u3001\u65e5\u5fd7\u91cc\u53c8\u6ca1\u6709 warning&#xff0c;\u53ef\u4ee5\u5148\u68c0\u67e5 mapper \u4e2d\u6709\u6ca1\u6709 DISTINCT\u3001GROUP BY\u3001UNION&#xff0c;\u4ee5\u53ca select \u5217\u8868\u91cc\u7684\u53c2\u6570\u3002\u4e5f\u53ef\u4ee5\u4ece MyBatis \u7684 Preparing \u65e5\u5fd7\u4e2d\u641c\u7d22 COUNT(*) FROM (&#xff0c;\u770b\u6700\u7ec8\u6267\u884c\u7684 count \u662f\u4e0d\u662f\u5916\u5c42\u5305\u88c5\u5f62\u5f0f\u3002<\/p>\n<h3>\u56db\u3001LEFT JOIN \u6709\u65f6\u80fd\u4ece count \u91cc\u79fb\u9664&#xff0c;INNER JOIN \u901a\u5e38\u4f1a\u4fdd\u7559<\/h3>\n<p>\u518d\u770b\u4e00\u7ec4 JOIN \u7684\u5bf9\u6bd4\u3002\u8fd9\u91cc\u4e3b\u8981\u60f3\u8bf4\u660e&#xff1a;\u5206\u9875 SQL \u91cc\u6709 JOIN&#xff0c;\u4e0d\u4ee3\u8868 count \u4e00\u5b9a\u4f1a\u6309\u539f\u6837\u6267\u884c&#xff0c;\u4f46 JOIN \u88ab\u4fdd\u7559\u65f6&#xff0c;count \u4e5f\u8981\u628a\u660e\u7ec6\u8868\u4e00\u8d77\u7b97\u8fdb\u6765&#xff0c;\u8017\u65f6\u660e\u663e\u589e\u52a0\u3002<\/p>\n<p><span class=\"token comment\">&#8212; LEFT JOIN&#xff1a;\u672c\u6b21\u6d4b\u8bd5\u4e2d count \u4f18\u5316\u65f6\u79fb\u9664\u4e86\u660e\u7ec6\u8868<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> o<span class=\"token punctuation\">.<\/span>id<span class=\"token punctuation\">,<\/span> o<span class=\"token punctuation\">.<\/span>amount<span class=\"token punctuation\">,<\/span> i<span class=\"token punctuation\">.<\/span>product_id<br \/>\n<span class=\"token keyword\">FROM<\/span> orders o<br \/>\n<span class=\"token keyword\">LEFT<\/span> <span class=\"token keyword\">JOIN<\/span> order_items i <span class=\"token keyword\">ON<\/span> o<span class=\"token punctuation\">.<\/span>id <span class=\"token operator\">&#061;<\/span> i<span class=\"token punctuation\">.<\/span>order_id<br \/>\n<span class=\"token keyword\">WHERE<\/span> o<span class=\"token punctuation\">.<\/span><span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> ?<\/p>\n<p><span class=\"token comment\">&#8212; INNER JOIN&#xff1a;\u672c\u6b21\u6d4b\u8bd5\u4e2d count \u4fdd\u7559\u4e86\u660e\u7ec6\u8868<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> o<span class=\"token punctuation\">.<\/span>id<span class=\"token punctuation\">,<\/span> o<span class=\"token punctuation\">.<\/span>amount<span class=\"token punctuation\">,<\/span> i<span class=\"token punctuation\">.<\/span>qty<br \/>\n<span class=\"token keyword\">FROM<\/span> orders o<br \/>\n<span class=\"token keyword\">INNER<\/span> <span class=\"token keyword\">JOIN<\/span> order_items i <span class=\"token keyword\">ON<\/span> o<span class=\"token punctuation\">.<\/span>id <span class=\"token operator\">&#061;<\/span> i<span class=\"token punctuation\">.<\/span>order_id<br \/>\n<span class=\"token keyword\">WHERE<\/span> o<span class=\"token punctuation\">.<\/span><span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> ?<\/p>\n<p>left join \u90a3\u7ec4\u7684\u539f\u59cb SQL \u91cc&#xff0c;\u53f3\u8868\u5b57\u6bb5\u6ca1\u51fa\u73b0\u5728 where&#xff0c;on \u91cc\u4e5f\u6ca1\u6709\u53c2\u6570&#xff0c;count \u6539\u5199\u65f6\u6574\u6bb5 join \u5c31\u88ab\u6458\u6389\u4e86&#xff0c;\u8017\u65f6 63ms\u3002\u8fd9\u4e2a\u6570\u5b57\u8fd8\u662f\u6bd4\u666e\u901a\u7b49\u503c\u67e5\u8be2\u7684 7ms \u9ad8\u4e00\u622a&#xff0c;\u4f46\u539f\u56e0\u8ddf join \u6ca1\u5173\u7cfb&#xff0c;join \u90fd\u5df2\u7ecf\u5220\u4e86&#xff0c;\u5269\u4e0b\u7684 SQL \u53ea\u6709 status \u4e00\u4e2a\u6761\u4ef6&#xff0c;\u6ca1\u7528\u4e0a (biz_code, status) \u8054\u5408\u7d22\u5f15\u7684\u6700\u5de6\u5217&#xff0c;\u53ea\u80fd\u6309 status \u53bb\u626b\u3002<\/p>\n<p>inner join \u90a3\u7ec4\u53cd\u8fc7\u6765&#xff0c;\u660e\u7ec6\u8868\u5173\u8054\u539f\u5c01\u4e0d\u52a8\u7559\u5728 count \u91cc&#xff0c;\u8981\u628a 400 \u4e07\u884c\u660e\u7ec6\u771f\u7684\u8fc7\u4e00\u904d&#xff0c;645ms\u3002<\/p>\n<p>\u63d2\u4ef6\u5224\u65ad\u7684\u4f9d\u636e\u5176\u5b9e\u5f88\u7b80\u5355&#xff1a;\u8fd9\u6761 join \u662f\u4e0d\u662f left &#xff0c;\u53f3\u8868\u7684\u522b\u540d\u6709\u6ca1\u6709\u5728 where \u91cc\u51fa\u73b0\u8fc7&#xff0c;\u5b83\u662f\u62ff \u522b\u540d. \u53bb where \u91cc\u505a\u5b57\u7b26\u4e32\u5339\u914d&#xff0c;\u6240\u4ee5 where \u91cc\u4e0d\u5199\u522b\u540d\u7684\u6761\u4ef6\u5b83\u8ba4\u4e0d\u51fa\u6765&#xff0c;on \u91cc\u6709\u6ca1\u6709\u95ee\u53f7\u5360\u4f4d\u7b26&#xff0c;\u5e26\u95ee\u53f7\u8bf4\u660e\u8fd9\u6bb5 SQL \u5403\u53c2\u6570&#xff0c;\u5220\u6389 join \u53c2\u6570\u5c31\u5bf9\u4e0d\u4e0a\u4e86\u3002<\/p>\n<p>\u4f46\u8fd9\u4e9b\u90fd\u53ea\u662f\u8bed\u6cd5\u5c42\u9762\u7684\u5224\u65ad\u3002\u63d2\u4ef6\u4e0d\u8fde\u6570\u636e\u5e93&#xff0c;\u4e0d\u77e5\u9053 orders \u548c order_items \u662f\u4e00\u5bf9\u591a\u8fd8\u662f\u591a\u5bf9\u4e00\u3002\u6211\u8fd9\u4e24\u5f20\u8868\u6b63\u662f\u4e00\u5bf9\u591a&#xff0c;\u5982\u679c\u54ea\u5929\u5217\u8868\u9875\u5199\u6210 left join \u660e\u7ec6\u3001where \u53ea\u7b5b\u4e3b\u8868\u3001on \u91cc\u4e5f\u6ca1\u53c2\u6570&#xff0c;\u5b83\u7167\u6837\u4f1a\u628a join \u5220\u6389&#xff0c;count \u5c31\u4ece\u660e\u7ec6\u884c\u6570\u53d8\u6210\u8ba2\u5355\u6570\u3002\u4e0d\u62a5\u9519&#xff0c;\u4e5f\u4e0d warning&#xff0c;\u4f60\u53ea\u4f1a\u53d1\u73b0\u5206\u9875\u7684\u603b\u6570\u6bd4\u5b9e\u9645\u5c11\u4e86\u4e00\u622a\u3002<\/p>\n<p>\u6240\u4ee5\u8fd9\u7c7b\u5217\u8868\u6211\u66f4\u613f\u610f\u62c6\u5f00\u67e5&#xff1a;\u5148\u6309\u6761\u4ef6\u5206\u9875 orders&#xff0c;\u62ff\u5230\u5f53\u524d\u9875\u90a3\u51e0\u5341\u4e2a id&#xff0c;\u518d\u7528 in \u628a\u660e\u7ec6\u4e00\u6b21\u635e\u56de\u6765\u3002\u603b\u6570\u67e5\u8be2\u53ea\u9762\u5bf9\u4e3b\u8868&#xff0c;\u4e0d\u7528\u4e3a\u4e86\u5c55\u793a\u51e0\u4e2a\u5b57\u6bb5\u628a\u6574\u5f20\u660e\u7ec6\u8868\u62d6\u8fdb count\u3002<\/p>\n<p>\u60f3\u77e5\u9053\u5b83\u5230\u5e95\u5220\u6ca1\u5220&#xff0c;\u4e5f\u4e0d\u7528\u731c&#xff0c;\u628a SQL \u65e5\u5fd7\u6253\u5f00&#xff0c;\u770b\u5b83\u5b9e\u9645\u751f\u6210\u7684\u90a3\u6761 count \u5c31\u884c\u3002<\/p>\n<h3>\u4e94\u3001\u56db\u79cd\u4fee\u6cd5&#xff0c;\u5206\u522b\u89e3\u51b3\u4e0d\u540c\u7684\u95ee\u9898<\/h3>\n<p>\u524d\u9762\u770b\u4e86\u51e0\u79cd count \u53d8\u6162\u7684\u60c5\u51b5&#xff0c;\u4e0b\u9762\u662f\u672c\u6b21\u5b9e\u9a8c\u91cc\u5c1d\u8bd5\u7684\u56db\u79cd\u5904\u7406\u65b9\u5f0f\u3002\u5b83\u4eec\u4e0d\u662f\u56db\u4e2a\u53ef\u4ee5\u968f\u4fbf\u4e92\u6362\u7684\u5f00\u5173&#xff0c;\u9002\u7528\u6761\u4ef6\u4e0d\u4e00\u6837\u3002<\/p>\n<h4>\u65b9\u6848\u4e00&#xff1a;\u628a BINARY \u6539\u6210 COLLATE<\/h4>\n<p>\u539f\u6765&#xff1a;<\/p>\n<p><span class=\"token string\">&#034;&#8230; AND BINARY biz_code &#061; #{biz} &#8230;&#034;<\/span><\/p>\n<p>\u8c03\u6574\u540e&#xff1a;<\/p>\n<p><span class=\"token string\">&#034;&#8230; AND biz_code &#061; #{biz} COLLATE utf8mb4_bin &#8230;&#034;<\/span><\/p>\n<p>MySQL \u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528 idx_biz_status \u8986\u76d6\u7d22\u5f15&#xff0c;JSqlParser \u4e5f\u80fd\u89e3\u6790\u8fd9\u6761 SQL&#xff0c;count \u4ece\u7ea6 340ms \u964d\u5230\u4e86 9ms\u3002<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6848\u9700\u8981\u6ce8\u610f\u6bd4\u8f83\u8bed\u4e49\u3002COLLATE utf8mb4_bin \u662f\u4e8c\u8fdb\u5236\u6392\u5e8f\u89c4\u5219\u6bd4\u8f83&#xff0c;\u5927\u5c0f\u5199\u654f\u611f&#xff0c;\u4f46\u5b83\u548c BINARY \u5e76\u4e0d\u662f\u6240\u6709\u7ec6\u8282\u90fd\u5b8c\u5168\u76f8\u540c\u3002<\/p>\n<p>\u66f4\u5f7b\u5e95\u7684\u65b9\u5f0f\u662f\u8c03\u6574\u5217\u672c\u8eab\u7684\u6392\u5e8f\u89c4\u5219&#xff0c;\u8ba9 SQL \u4e0d\u5fc5\u6bcf\u6b21\u5199 BINARY \u6216 COLLATE\u3002\u4f46\u8fd9\u4f1a\u5f71\u54cd\u6574\u5f20\u8868\u5176\u4ed6\u67e5\u8be2\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u8bed\u4e49&#xff0c;\u6539\u4e4b\u524d\u8981\u786e\u8ba4\u4e1a\u52a1\u662f\u5426\u4f9d\u8d56\u539f\u6765\u7684\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u884c\u4e3a\u3002<\/p>\n<h4>\u65b9\u6848\u4e8c&#xff1a;\u4e3a\u8fd9\u6761\u67e5\u8be2\u5355\u72ec\u5199 count<\/h4>\n<p>\u5982\u679c\u5217\u8868 SQL \u672c\u8eab\u6709\u590d\u6742\u5206\u7ec4&#xff0c;\u800c\u4e1a\u52a1\u53c8\u80fd\u660e\u786e\u5199\u51fa\u7b49\u4ef7\u7684\u7edf\u8ba1 SQL&#xff0c;\u53ef\u4ee5\u4f7f\u7528 MyBatis-Plus \u7684 countId \u6307\u5b9a\u81ea\u5b9a\u4e49 count\u3002<\/p>\n<p><span class=\"token class-name\">Page<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">Map<\/span><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">Object<\/span><span class=\"token punctuation\">&gt;<\/span><span class=\"token punctuation\">&gt;<\/span><\/span> page <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Page<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token punctuation\">&gt;<\/span><\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\npage<span class=\"token punctuation\">.<\/span><span class=\"token function\">setCountId<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;selectGroupCount&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5bf9\u5e94\u7684 mapper XML&#xff1a;<\/p>\n<p><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>select<\/span> <span class=\"token attr-name\">id<\/span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">&#061;<\/span><span class=\"token punctuation\">&#034;<\/span>selectGroupCount<span class=\"token punctuation\">&#034;<\/span><\/span> <span class=\"token attr-name\">resultType<\/span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">&#061;<\/span><span class=\"token punctuation\">&#034;<\/span>java.lang.Long<span class=\"token punctuation\">&#034;<\/span><\/span><span class=\"token punctuation\">&gt;<\/span><\/span><br \/>\n    SELECT COUNT(DISTINCT user_id)<br \/>\n    FROM orders<br \/>\n    WHERE status &#061; #{status}<br \/>\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;\/<\/span>select<\/span><span class=\"token punctuation\">&gt;<\/span><\/span><\/p>\n<p>GROUP BY \u67e5\u8be2\u7684 count \u4ece\u7ea6 2.1 \u79d2\u964d\u5230\u4e86 50ms\u3002\u8fd9\u91cc\u80fd\u7528 COUNT(DISTINCT user_id)&#xff0c;\u662f\u56e0\u4e3a\u539f\u67e5\u8be2\u6309 user_id \u5206\u7ec4&#xff0c;\u603b\u6570\u5c31\u662f\u4e0d\u540c user_id \u7684\u6570\u91cf\u3002<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6848\u4e0d\u80fd\u7167\u642c\u5230\u6240\u6709\u5206\u7ec4\u67e5\u8be2\u3002\u4f8b\u5982\u6309\u5929\u5206\u7ec4\u5c55\u793a\u8ba2\u5355&#xff0c;\u4f46\u9875\u9762\u603b\u6570\u8981\u663e\u793a\u8ba2\u5355\u6761\u6570&#xff0c;\u90a3\u4e48 COUNT(DISTINCT user_id) \u5c31\u5b8c\u5168\u4e0d\u662f\u540c\u4e00\u4e2a\u7edf\u8ba1\u53e3\u5f84\u3002\u6539\u4e4b\u524d\u5148\u628a\u539f\u67e5\u8be2\u7ed3\u679c\u7684\u884c\u6570\u548c\u81ea\u5b9a\u4e49 count \u5bf9\u4e00\u904d&#xff0c;\u786e\u4fdd\u8bed\u4e49\u76f8\u540c\u3002<\/p>\n<p>\u8fd8\u6709\u4e2a\u5c0f\u7ec6\u8282&#xff1a;countId \u6307\u5411\u7684\u8bed\u53e5\u8981\u653e\u5728\u540c\u4e00\u4e2a mapper namespace \u4e0b&#xff0c;\u5e76\u4e14\u5b83\u548c\u5206\u9875\u65b9\u6cd5\u5171\u7528\u53c2\u6570\u5bf9\u8c61\u3002\u5206\u9875\u65b9\u6cd5\u63a5\u6536 status&#xff0c;\u81ea\u5b9a\u4e49 count \u91cc\u4e5f\u8981\u4f7f\u7528\u5bf9\u5e94\u7684 #{status}&#xff0c;\u53c2\u6570\u540d\u4e0d\u4e00\u81f4\u4f1a\u76f4\u63a5\u51fa\u73b0\u7ed1\u5b9a\u5f02\u5e38\u3002<\/p>\n<p>\u53e6\u5916&#xff0c;count \u4ece 2.1 \u79d2\u964d\u5230 50ms&#xff0c;\u4e0d\u4ee3\u8868\u6574\u4e2a\u5206\u9875\u63a5\u53e3\u5c31\u53ea\u5269 50ms\u3002\u672c\u6b21\u5206\u9875\u67e5\u8be2\u672c\u8eab\u4ecd\u7136\u9700\u8981\u7ea6 0.6&#xff5e;0.8 \u79d2&#xff0c;\u81ea\u5b9a\u4e49 count \u53ea\u89e3\u51b3\u4e86 count \u8fd9\u4e00\u534a\u3002<\/p>\n<h4>\u65b9\u6848\u4e09&#xff1a;\u8865\u4e00\u4e2a\u8054\u5408\u7d22\u5f15<\/h4>\n<p><span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> orders<br \/>\n<span class=\"token keyword\">ADD<\/span> <span class=\"token keyword\">INDEX<\/span> idx_status_biz <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">status<\/span><span class=\"token punctuation\">,<\/span> biz_code<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>BINARY \u67e5\u8be2\u7684 count \u4ece\u7ea6 340ms \u964d\u5230\u4e86 108ms&#xff0c;\u6709\u6539\u5584&#xff0c;\u4f46\u6ca1\u6709\u56de\u5230 10ms \u4ee5\u5185\u3002<\/p>\n<p>\u4ece\u6267\u884c\u8ba1\u5212\u770b&#xff0c;\u539f\u6765\u7684\u5168\u8868\u626b\u63cf\u53d8\u6210\u4e86\u901a\u8fc7 status \u505a Index lookup&#xff0c;\u4f30\u7b97\u5019\u9009\u884c\u4ece 200 \u4e07\u7f29\u5c0f\u5230\u7ea6 65.7 \u4e07&#xff1b;\u4f46 BINARY biz_code \u4ecd\u7136\u662f\u8fc7\u6ee4\u6761\u4ef6&#xff0c;\u4e0d\u80fd\u50cf\u666e\u901a\u7b49\u503c\u6761\u4ef6\u90a3\u6837\u76f4\u63a5\u5229\u7528\u7d22\u5f15\u5b9a\u4f4d&#xff0c;\u5b9e\u9645\u8fc7\u6ee4\u540e\u5269 33,348 \u884c\u3002<\/p>\n<p>\u6240\u4ee5\u8865\u7d22\u5f15\u6709\u5e2e\u52a9&#xff0c;\u4f46\u6ca1\u6709\u6d88\u9664 BINARY \u5e26\u6765\u7684\u95ee\u9898\u3002\u7d22\u5f15\u4e5f\u4e0d\u662f\u514d\u8d39\u7684&#xff1a;\u5199\u5165\u65f6\u9700\u8981\u989d\u5916\u7ef4\u62a4\u7d22\u5f15&#xff0c;200 \u4e07\u884c\u7684\u8868\u589e\u52a0\u7d22\u5f15\u8fd8\u8981\u5b89\u6392\u5408\u9002\u7684\u53d8\u66f4\u7a97\u53e3\u3002<\/p>\n<h4>\u65b9\u6848\u56db&#xff1a;\u5982\u679c\u9875\u9762\u4e0d\u9700\u8981\u603b\u6570&#xff0c;\u5c31\u4e0d\u8981\u67e5 count<\/h4>\n<p><span class=\"token class-name\">Page<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">Map<\/span><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">Object<\/span><span class=\"token punctuation\">&gt;<\/span><span class=\"token punctuation\">&gt;<\/span><\/span> page <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Page<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token punctuation\">&gt;<\/span><\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\npage<span class=\"token punctuation\">.<\/span><span class=\"token function\">setSearchCount<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5173\u95ed count \u540e&#xff0c;\u539f\u6765\u7ea6 340ms \u7684 count \u5f00\u9500\u5c31\u6ca1\u6709\u4e86&#xff0c;\u5206\u9875\u6570\u636e\u67e5\u8be2\u7ea6 5ms\u3002<\/p>\n<p>\u8fd9\u9002\u5408\u65e0\u9650\u6eda\u52a8\u3001\u53ea\u9700\u8981&#034;\u52a0\u8f7d\u66f4\u591a&#034;\u7684\u5217\u8868&#xff0c;\u6216\u8005\u603b\u6570\u7531\u53e6\u4e00\u4e2a\u7edf\u8ba1\u63a5\u53e3\u63d0\u4f9b\u7684\u573a\u666f\u3002\u5982\u679c\u4ea7\u54c1\u9875\u9762\u9700\u8981\u5c55\u793a&#034;\u5171 N \u6761&#034;&#xff0c;\u5c31\u4e0d\u80fd\u7b80\u5355\u5173\u95ed count\u3002<\/p>\n<h4>\u56db\u79cd\u65b9\u6848\u653e\u5728\u4e00\u8d77\u770b\u4e00\u4e0b<\/h4>\n<table>\n<tr>\u65b9\u6848\u5bf9\u5e94\u5199\u6cd5count \u8017\u65f6\u4e3b\u8981\u4ee3\u4ef7\u6216\u9650\u5236<\/tr>\n<tbody>\n<tr>\n<td>\u539f\u59cb\u666e\u901a\u7b49\u503c\u67e5\u8be2<\/td>\n<td>\u5bf9\u7167\u7ec4<\/td>\n<td align=\"right\">7ms<\/td>\n<td>\u2014<\/td>\n<\/tr>\n<tr>\n<td>\u6362 COLLATE<\/td>\n<td>BINARY \u67e5\u8be2&#xff08;340ms&#xff09;<\/td>\n<td align=\"right\">9ms<\/td>\n<td>\u6bd4\u8f83\u8bed\u4e49\u9700\u8981\u6838\u5bf9<\/td>\n<\/tr>\n<tr>\n<td>\u81ea\u5b9a\u4e49 count<\/td>\n<td>GROUP BY \u67e5\u8be2&#xff08;2.1s&#xff09;<\/td>\n<td align=\"right\">50ms<\/td>\n<td>\u5fc5\u987b\u4fdd\u8bc1\u7edf\u8ba1\u53e3\u5f84\u7b49\u4ef7<\/td>\n<\/tr>\n<tr>\n<td>\u8865\u7d22\u5f15<\/td>\n<td>BINARY \u67e5\u8be2&#xff08;340ms&#xff09;<\/td>\n<td align=\"right\">108ms<\/td>\n<td>\u4ecd\u9700\u626b\u63cf\u548c\u8fc7\u6ee4\u8f83\u591a\u5019\u9009\u884c<\/td>\n<\/tr>\n<tr>\n<td>\u5173\u95ed count<\/td>\n<td>BINARY \u67e5\u8be2&#xff08;340ms&#xff09;<\/td>\n<td align=\"right\">\u4e0d\u6267\u884c count<\/td>\n<td>\u9875\u9762\u4e0d\u518d\u76f4\u63a5\u83b7\u5f97\u603b\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u51e0\u79cd\u65b9\u6848\u91cc&#xff0c;\u5982\u679c\u4e1a\u52a1\u5141\u8bb8\u4fee\u6539 SQL&#xff0c;\u4f18\u5148\u8003\u8651\u628a SQL \u5199\u6cd5\u8c03\u6574\u5230\u65e2\u80fd\u88ab\u89e3\u6790\u5668\u5904\u7406\u3001\u53c8\u80fd\u6b63\u5e38\u4f7f\u7528\u7d22\u5f15\u7684\u5f62\u5f0f\u3002\u81ea\u5b9a\u4e49 count \u9002\u5408\u7edf\u8ba1\u53e3\u5f84\u660e\u786e\u7684\u590d\u6742\u67e5\u8be2&#xff1b;\u8865\u7d22\u5f15\u53ef\u4ee5\u7f13\u89e3\u626b\u63cf\u8303\u56f4&#xff0c;\u4f46\u4e0d\u4e00\u5b9a\u80fd\u89e3\u51b3\u8868\u8fbe\u5f0f\u5bfc\u81f4\u7684\u7d22\u5f15\u4f7f\u7528\u95ee\u9898&#xff1b;\u53ea\u6709\u4ea7\u54c1\u4e0d\u9700\u8981\u603b\u6570\u65f6&#xff0c;\u624d\u8003\u8651\u5173\u95ed count\u3002<\/p>\n<h3>\u516d\u3001\u5b9e\u9645\u6392\u67e5\u65f6&#xff0c;\u6211\u4f1a\u5148\u770b\u8fd9\u51e0\u5904<\/h3>\n<p>\u5982\u679c\u9879\u76ee\u91cc\u4e5f\u51fa\u73b0\u4e86 optimize this sql to a count sql has exception&#xff0c;\u53ef\u4ee5\u6309\u4e0b\u9762\u7684\u987a\u5e8f\u67e5&#xff1a;<\/p>\n<li>\u5148\u627e count SQL\u3002 \u770b warning \u540e\u9762\u6253\u5370\u7684\u662f\u54ea\u6761 SQL&#xff0c;\u518d\u4ece MyBatis Preparing \u65e5\u5fd7\u91cc\u627e\u5230\u5bf9\u5e94\u7684 count \u8bed\u53e5\u3002<\/li>\n<li>\u770b\u6700\u7ec8\u6709\u6ca1\u6709\u964d\u7ea7\u3002 \u5982\u679c\u6267\u884c SQL \u662f SELECT COUNT(*) FROM (&#8230;) TOTAL&#xff0c;\u8bf4\u660e\u4f7f\u7528\u4e86\u5916\u5c42\u5305\u88c5\u5f62\u5f0f\u3002<\/li>\n<li>\u770b\u4e3a\u4ec0\u4e48\u6ca1\u6539\u5199\u3002 \u6709 warning \u65f6&#xff0c;\u5148\u5b9a\u4f4d JSqlParser \u62a5\u9519\u7684 token&#xff1b;\u6ca1\u6709 warning&#xff0c;\u5219\u68c0\u67e5 DISTINCT\u3001GROUP BY\u3001UNION \u7b49\u7ed3\u6784\u5206\u652f\u3002<\/li>\n<li>\u770b\u6570\u636e\u5e93\u5b9e\u9645\u600e\u4e48\u6267\u884c\u3002 \u5bf9 count SQL \u5355\u72ec\u6267\u884c EXPLAIN ANALYZE&#xff0c;\u786e\u8ba4\u662f\u7d22\u5f15\u626b\u63cf\u3001\u5168\u8868\u626b\u63cf\u3001\u5206\u7ec4\u8fd8\u662f JOIN \u5e26\u6765\u4e86\u4e3b\u8981\u5f00\u9500\u3002<\/li>\n<li>\u6700\u540e\u518d\u9009\u4fee\u6cd5\u3002 \u80fd\u6539 SQL \u5c31\u5148\u6539 SQL&#xff1b;\u7edf\u8ba1\u53e3\u5f84\u80fd\u5355\u72ec\u8868\u8fbe\u5c31\u8003\u8651\u81ea\u5b9a\u4e49 count&#xff1b;\u786e\u8ba4\u9875\u9762\u4e0d\u9700\u8981\u603b\u6570&#xff0c;\u518d\u5173\u95ed count\u3002<\/li>\n<p>\u6709\u4e2a\u5730\u65b9\u503c\u5f97\u8bb0\u4f4f&#xff1a;\u65e5\u5fd7\u6ca1\u6709 warning&#xff0c;\u4e0d\u4ee3\u8868 count \u4e00\u5b9a\u4f18\u5316\u6210\u529f\u3002\u9759\u9ed8\u964d\u7ea7\u7684\u573a\u666f&#xff0c;\u76f4\u63a5\u770b\u6700\u7ec8\u6267\u884c\u7684 SQL \u66f4\u53ef\u9760\u3002\u7ebf\u4e0a\u4e0d\u65b9\u4fbf\u9010\u6761\u7ffb\u65e5\u5fd7\u65f6&#xff0c;\u53ef\u4ee5\u641c\u7d22 COUNT(*) FROM ( \u4f5c\u4e3a\u7ebf\u7d22&#xff0c;\u518d\u7ed3\u5408 mapper \u548c\u6267\u884c\u8ba1\u5212\u786e\u8ba4\u3002<\/p>\n<h3>\u4e03\u3001\u5c0f\u7ed3<\/h3>\n<p>\u5206\u9875\u6162\u7684\u65f6\u5019&#xff0c;\u4e0d\u8981\u53ea\u76ef\u7740\u5e26 LIMIT \u7684\u90a3\u6761 SQL\u3002\u5148\u770b count \u6700\u7ec8\u751f\u6210\u4e86\u4ec0\u4e48&#xff0c;\u518d\u770b\u6267\u884c\u8ba1\u5212&#xff1b;\u6709 warning \u67e5\u89e3\u6790\u5931\u8d25\u7684\u539f\u56e0&#xff0c;\u6ca1\u6709 warning \u4e5f\u8981\u7559\u610f\u9759\u9ed8\u964d\u7ea7\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4e5f\u9047\u5230\u8fc7 MyBatis-Plus \u5206\u9875 count \u7279\u522b\u6162&#xff0c;\u53ef\u4ee5\u5148\u770b\u4e00\u4e0b mapper \u91cc\u7684 SQL \u6709\u6ca1\u6709 BINARY\u3001DISTINCT\u3001GROUP BY \u6216 UNION\u3002\u5f88\u591a\u65f6\u5019&#xff0c;\u95ee\u9898\u4e0d\u5728\u5206\u9875\u63d2\u4ef6\u7a81\u7136\u53d8\u6162&#xff0c;\u800c\u662f\u5b83\u4e3a\u4e86\u4fdd\u8bc1\u7edf\u8ba1\u7ed3\u679c\u6b63\u786e&#xff0c;\u9009\u62e9\u4e86\u4e00\u6761\u66f4\u4fdd\u5b88\u3001\u4f46\u6267\u884c\u4ee3\u4ef7\u66f4\u9ad8\u7684 count\u3002<\/p>\n<p>\u7cfb\u5217&#xff1a;\u76f8\u5173 ai-launch-check \u5b9e\u6d4b&#xff1a;LangChain4j \u4ed3\u4e0a\u7ebf\u524d\u626b\u63cf&#xff0c;25 \u6761\u547d\u4e2d 2 \u6761\u662f\u5b83\u770b\u9519&#xff5c;AI \u5ba2\u670d\u7cfb\u7edf\u7684\u5ba1\u8ba1\u65e5\u5fd7&#xff1a;\u8bb0\u4ec0\u4e48\u3001\u5b58\u591a\u4e45\u3001\u600e\u4e48\u67e5&#xff5c;\u6280\u672f\u7eb5\u6df1\u7cfb\u5217\u7d22\u5f15<\/p>\n<p>      <span id=\"quality-guide-article-end\" aria-hidden=\"true\" style=\"display:block;height:1px;margin-bottom:-1px\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9098,99,1769,114,292,56],"topic":[],"class_list":["post-124514","post","type-post","status-publish","format-standard","hentry","category-server","tag-it","tag-java","tag-mybatis","tag-mysql","tag-292","tag-56"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MyBatis-Plus \u5206\u9875\u7684 count \u67e5\u8be2\u88ab\u964d\u7ea7\u4e86\uff1a\u90a3\u884c optimize this sql\uff0c\u4e0d\u662f\u8ba9\u4f60\u5173\u65e5\u5fd7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/124514.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MyBatis-Plus \u5206\u9875\u7684 count \u67e5\u8be2\u88ab\u964d\u7ea7\u4e86\uff1a\u90a3\u884c optimize this sql\uff0c\u4e0d\u662f\u8ba9\u4f60\u5173\u65e5\u5fd7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/124514.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-18T19:08:36+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/124514.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/124514.html\",\"name\":\"MyBatis-Plus \u5206\u9875\u7684 count \u67e5\u8be2\u88ab\u964d\u7ea7\u4e86\uff1a\u90a3\u884c optimize this sql\uff0c\u4e0d\u662f\u8ba9\u4f60\u5173\u65e5\u5fd7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-11-18T19:08:36+00:00\",\"dateModified\":\"2026-11-18T19:08:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/124514.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/124514.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/124514.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MyBatis-Plus \u5206\u9875\u7684 count \u67e5\u8be2\u88ab\u964d\u7ea7\u4e86\uff1a\u90a3\u884c optimize this sql\uff0c\u4e0d\u662f\u8ba9\u4f60\u5173\u65e5\u5fd7\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MyBatis-Plus \u5206\u9875\u7684 count \u67e5\u8be2\u88ab\u964d\u7ea7\u4e86\uff1a\u90a3\u884c optimize this sql\uff0c\u4e0d\u662f\u8ba9\u4f60\u5173\u65e5\u5fd7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/124514.html","og_locale":"zh_CN","og_type":"article","og_title":"MyBatis-Plus \u5206\u9875\u7684 count \u67e5\u8be2\u88ab\u964d\u7ea7\u4e86\uff1a\u90a3\u884c optimize this sql\uff0c\u4e0d\u662f\u8ba9\u4f60\u5173\u65e5\u5fd7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content","og_url":"https:\/\/www.wsisp.com\/helps\/124514.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-11-18T19:08:36+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/124514.html","url":"https:\/\/www.wsisp.com\/helps\/124514.html","name":"MyBatis-Plus \u5206\u9875\u7684 count \u67e5\u8be2\u88ab\u964d\u7ea7\u4e86\uff1a\u90a3\u884c optimize this sql\uff0c\u4e0d\u662f\u8ba9\u4f60\u5173\u65e5\u5fd7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-11-18T19:08:36+00:00","dateModified":"2026-11-18T19:08:36+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/124514.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/124514.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/124514.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"MyBatis-Plus \u5206\u9875\u7684 count \u67e5\u8be2\u88ab\u964d\u7ea7\u4e86\uff1a\u90a3\u884c optimize this sql\uff0c\u4e0d\u662f\u8ba9\u4f60\u5173\u65e5\u5fd7"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/124514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=124514"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/124514\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=124514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=124514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=124514"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=124514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}