{"id":45579,"date":"2023-04-11T17:42:39","date_gmt":"2023-04-11T09:42:39","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=45579"},"modified":"2023-04-11T17:42:39","modified_gmt":"2023-04-11T09:42:39","slug":"special-optimize-sql","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/special-optimize-sql.html","title":{"rendered":"\u8ba9\u4f60\u8111\u6d1e\u5927\u5f00\u7684MySQL\u4f18\u5316\u6280\u5de7"},"content":{"rendered":"
\u7531\u4e8e\u5206\u5e93\u5206\u8868\u7684\u539f\u56e0\uff0c\u548c\u5f00\u53d1\u89c4\u5b9a\u4e86\u4e0d\u80fd\u4f7f\u7528 \u8868\u8868JOIN \u8bed\u53e5\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u8981\u5c06 JOIN \u8bed\u53e5\u7684\u8f6c\u5316\u6210\u4f7f\u7528 IN \u6765\u505a\u3002\u5982\u73b0\u5728\u6709 \u8868 A(a_id, c_a)c_a\u6709\u666e\u901a\u7d22\u5f15,\u8868 B(b_id, c_a) \u8fd9\u4e24\u4e2a\u8868\u8981\u5173\u8054, \u5e94\u8be5\u8f6c\u5316\u4e3a\u4ee5\u4e0b\u6b65\u9aa4\u5904\u7406:<\/p>\n
SELECT c_a FROM B WHERE xxx;<\/pre>\n
SELECT a_id, ... FROM A WHERE c_a IN(\u5728 1 \u4e2d\u67e5\u51fa\u6765\u7684 c_a)<\/pre>\n\u573a\u666f<\/strong><\/div>\n\u73b0\u5728\u8868\u7684\u6570\u636e\u91cf\u6709 800\u4e07\u3002<\/p>\n
\u4e00\u822c\u7684\u4f7f\u7528\u8bed\u53e5\u662f\uff1a<\/p>\n
SELECT * FROM A WHERE c_a IN(955555, 955556, 955557, 955558, 955559);<\/pre>\n\u4e0a\u9762\u8bed\u53e5\u4f1a\u6267\u884c\u7684\u5f88\u5feb\uff0c\u77e5\u9053\u4f7f\u7528 explain \u7684\u90fd\u660e\u767d\u8fd9\u6837\u4e00\u822c\u90fd\u662f\u4f1a\u4f7f\u7528\u7d22\u5f15\u7684\uff0c\u5e76\u4e14\u662f\u6240\u6709\u8303\u56f4\u626b\u63cf\u3002<\/p>\n
MySQL\u4e0d\u4f1a\u4ece 1 \u5f00\u59cb \u626b\u63cf 800\u4e07\uff0c\u800c\u662f\u4ece555555 \u626b\u63cf\u5230 555559(\u53ea\u8981\u626b\u63cf5\u884c\u6570\u636e)\u3002<\/p>\n
\u5728\u4e00\u822c\u60c5\u51b5\u4e0b\u662f\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898\u7684\u3002\u4f46\u662f\u5982\u679c IN \u91cc\u9762\u7684\u6570\u636e\u662f\u4e0d\u8fde\u7eed\u7684\u5c31\u6709\u5f88\u5927\u95ee\u9898\u4e86\u3002<\/p>\n
\u521b\u5efa\u8868\u7ed3\u6784\u8bed\u53e5<\/strong><\/div>\nCREATE TABLE t(\r\n id INT unsigned NOT NULL AUTO_INCREMENT,\r\n cid INT unsigned NOT NULL DEFAULT 0,\r\n c1 VARCHAR(50) NOT NULL DEFAULT '',\r\n c2 VARCHAR(50) NOT NULL DEFAULT '',\r\n c3 VARCHAR(50) NOT NULL DEFAULT '',\r\n c4 VARCHAR(50) NOT NULL DEFAULT '',\r\n c5 VARCHAR(50) NOT NULL DEFAULT '',\r\n c6 VARCHAR(50) NOT NULL DEFAULT '',\r\n PRIMARY KEY(id),\r\n INDEX idx$cid(cid)\r\n);\r\n \r\nINSERT INTO t VALUES(\r\n NULL,\r\n FLOOR(RAND() * 1000000),\r\n REPEAT('a', 50),\r\n REPEAT('a', 50),\r\n REPEAT('a', 50),\r\n REPEAT('a', 50),\r\n REPEAT('a', 50),\r\n REPEAT('a', 50)\r\n);\r\n \r\n-- \u91cd\u590d\u6267\u884c\r\nINSERT INTO t\r\nSELECT NULL,\r\n FLOOR(RAND() * 1000000),\r\n c1,\r\n c2,\r\n c3,\r\n c4,\r\n c5,\r\n c6\r\nFROM t;<\/pre>\n\u4e0b\u9762\u662f\u5177\u4f53\u7684\u5b9e\u9a8c\u8fc7\u7a0b<\/strong><\/div>\n\n
- \u4f7f\u7528IN\u67e5\u8be2\u8fde\u7eed\u7684\u6570<\/li>\n<\/ul>\n
SELECT *\r\nFROM t\r\nWHERE cid IN(955555, 955556, 955557, 955558, 955559);\r\n+---------+--------+-----------------------------------\r\n| id | cid | c1 \r\n+---------+--------+-----------------------------------\r\n| 319330 | 955555 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n| 1885293 | 955555 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n| ......\r\n| 8733757 | 955559 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n| 8796305 | 955559 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n+---------+--------+-----------------------------------\r\n41 rows in set (0.15 sec)<\/pre>\n\n
- \u4f7f\u7528IN\u67e5\u8be2\u4e0d\u8fde\u7eed\u7684\u6570<\/li>\n<\/ul>\n
SELECT *\r\nFROM t\r\nWHERE cid IN(1, 5000, 50000, 500000, 955559);\r\n+---------+--------+-----------------------------------\r\n| id | cid | c1 \r\n+---------+--------+-----------------------------------\r\n| 1 | 341702 | 1 | aaaaaaaaaaaaaaaaaaaaaaaaa\r\n| 1 | 1045176 | 1 | aaaaaaaaaaaaaaaaaaaaaaaaa\r\n......\r\n| 955559 | 8733757 | 955559 | aaaaaaaaaaaaaaaaaaaaaaaaa\r\n| 955559 | 8796305 | 955559 | aaaaaaaaaaaaaaaaaaaaaaaaa\r\n+--------+---------+--------+--------------------------\r\n41 rows in set (4.34 sec)<\/pre>\n\n
- \u4f7f\u7528UNION\u4f18\u5316<\/li>\n<\/ul>\n
SELECT *\r\nFROM (\r\n SELECT 1 AS cid UNION ALL\r\n SELECT 5000 UNION ALL\r\n SELECT 50000 UNION ALL\r\n SELECT 500000 UNION ALL\r\n SELECT 955559\r\n) AS tmp, t\r\nWHERE tmp.cid = t.cid;\r\n+---------+--------+-----------------------------------\r\n| id | cid | c1 \r\n+---------+--------+-----------------------------------\r\n| 1 | 341702 | 1 | aaaaaaaaaaaaaaaaaaaaaaaaa\r\n| 1 | 1045176 | 1 | aaaaaaaaaaaaaaaaaaaaaaaaa\r\n......\r\n| 955559 | 8733757 | 955559 | aaaaaaaaaaaaaaaaaaaaaaaaa\r\n| 955559 | 8796305 | 955559 | aaaaaaaaaaaaaaaaaaaaaaaaa\r\n+--------+---------+--------+--------------------------\r\n41 rows in set (0.01 sec)<\/pre>\n\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u51fa\u4e0a\u9762\u4f7f\u7528UNION\u7684\u65b9\u6cd5\u751f\u6210\u4e00\u4e2a\u4e34\u65f6\u8868\u4f5c\u4e3a\u5173\u8054\u7684\u4e3b\u8868\u3002<\/p>\n
\u62d3\u5c55<\/strong><\/div>\n\u8981\u662fMySQL\u6709\u53ea\u5e26\u7684\u4e00\u4e2a\u884c\u8f6c\u5217\u7684\u51fd\u6570\u90a3\u5c31\u5b8c\u7f8e\u4e86\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u7528\u4f7f\u7528UNION\u4e86\u3002<\/p>\n
SELECT 1, 5000, 50000, 500000, 955559;\r\n+---+------+-------+--------+--------+\r\n| 1 | 5000 | 50000 | 500000 | 955559 |\r\n+---+------+-------+--------+--------+\r\n| 1 | 5000 | 50000 | 500000 | 955559 |\r\n+---+------+-------+--------+--------+\r\n1 row in set (0.00 sec)\r\n \r\n\u53d8\u6210\u4ee5\u4e0b\r\nSELECT row_to_col(1, 5000, 50000, 500000, 955559);\r\n+--------+\r\n| id |\r\n+--------+\r\n| 1 |\r\n| 5000 |\r\n| 50000 |\r\n| 500000 |\r\n| 955559 |\r\n+--------+<\/pre>\n\u8981\u662f\u80fd\u50cf\u4e0a\u9762\u5c31\u592a\u68d2\u4e86\u7b80\u76f4\u3002<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.ttlsa.com\/mysql\/mysql-sql-performance-let-you-head-big-open\/<\/a><\/p>\n