ãã©ã¡ã¼ã¿èª¿æ´ã§å¤ã ããã¿ããã§ããå¹çã®è¯ãSQLã«ã¤ãã¦æ¤è¨¼ãã¦ã¿ã¾ã。
ååã®æ¤è¨¼ã®ç¶ãã§ã。
æ¤è¨¼å 容ã¯ååã«ä¼¼ã¦ãã¾ã。
ãã£ã¨ããå®åçãªSQLã®ãã¥ã¼ãã³ã°ãã¯ããã¾ã。
æ¤è¨¼1
ãµã¤ãæ¯ã®åè¨viewæ°,conversionæ°ãè¨ç®
SQL1 | SELECT * FROM (SELECT site_id, site_name, SUM(view) AS view, SUM(conversion) AS conversion FROM site AS T1 INNER JOIN site_view AS T2 USING(site_id) WHERE date BETWEEN '2013/11/01' AND '2013/11/30' GROUP BY site_id, site_name ORDER BY site_id) AS S1 LIMIT 10 OFFSET 0; |
SQL2 | SELECT * FROM (SELECT site_id, site_name, view, conversion FROM site AS T1 INNER JOIN (SELECT site_id, SUM(view) AS view, SUM(conversion) AS conversion FROM site_view WHERE date BETWEEN '2013/11/01' AND '2013/11/30' GROUP BY site_id) AS T2 USING(site_id) ORDER BY site_id) AS S1 LIMIT 10 OFFSET 0; |
ååã¨ã»ã¼åä¸ãªã®ã§ã·ã§ã«ã®å®è¡çã¯çãã¾ã.
以ä¸ãæ¤è¨¼çµæã§ã。
å®è¡ãã¡ã¤ã« | 1åç® | 2åç® | 3åç® | 4åç® | 5åç® | 6åç® | 7åç® | 8åç® | 9åç® | 10åç® | æå° | æ大 | å¹³å |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sql1.sql | 417.611 | 376.637 | 420.722 | 350.816 | 316.651 | 322.067 | 335.734 | 307.371 | 385.674 | 370.905 | 307.371 | 420.722 | 360.418 |
sql2.sql | 321.847 | 280.912 | 303.027 | 263.667 | 258.189 | 276.157 | 310.888 | 274.68 | 277.269 | 265.227 | 258.189 | 321.847 | 283.186 |
ã¨æ¤è¨¼çµæã¯ãã®ããã«ãªãã¾ãã。
å ã»ã©ããããå·®ã¯ç¸®ã¾ãã¾ããããã¯ãSQLï¼ã®æ¹ãæ©ãã§ã。
ã§ã¯、SQL1ã®è¨è¿°ã¯é ãã®ãã親ã®ãã¼ã¿siteã«WHEREå¥ãããã¦è©¦ãã¾ã
SQL1 | SELECT * FROM (SELECT site_id, site_name, SUM(view) AS view, SUM(conversion) AS conversion FROM site AS T1 INNER JOIN site_view AS T2 USING(site_id) WHERE T1.site_name ~ '[0-5]$' GROUP BY site_id, site_name ORDER BY site_id) AS S1 LIMIT 10 OFFSET 0; |
SQL2 | SELECT * FROM (SELECT site_id, site_name, view, conversion FROM site AS T1 INNER JOIN (SELECT site_id, SUM(view) AS view, SUM(conversion) AS conversion FROM site_view GROUP BY site_id) AS T2 USING(site_id) WHERE T1.site_name ~ '[0-5]$' ORDER BY site_id) AS S1 LIMIT 10 OFFSET 0; |
ãã®ãããªSQLã§ã、ããã¦é ãããããã«æ£è¦è¡¨ç¾ã§è¨è¼
å®è¡ãã¡ã¤ã« | 1åç® | 2åç® | 3åç® | 4åç® | 5åç® | 6åç® | 7åç® | 8åç® | 9åç® | 10åç® | æå° | æ大 | å¹³å |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sql1.sql | 1709.609 | 1729.138 | 1714.069 | 1862.493 | 1838.969 | 1752.116 | 1668.097 | 1701.067 | 1749.651 | 1714.535 | 1668.097 | 1862.493 | 1743.974 |
sql2.sql | 1074.498 | 1056.323 | 1087.329 | 1060.741 | 1051.747 | 1133.521 | 1133.265 | 1143.876 | 1087.005 | 1065.277 | 1051.747 | 1143.876 | 1089.358 |
極端ã¨ã¾ã§ã¯è¡ããªããååè¿ãSQL2ãæ©ãè¯ãæ¸ãæ¹ã¯ãããã£ãããã«
å¹çåã§ãããã§ãã。
ã¨å¾ããï¼åæ¤è¨¼ããã・・・
HAVINGã使ããWHEREã使ããã§ãã
æ¤è¨¼ã§æ©ãã§ãããSQLï¼ããã¼ã¹ã«ä»¥ä¸ã®ãªãããã§æ¤è¨¼ãã¾ã
SQL1 | SELECT * FROM (SELECT site_id, site_name, view, conversion FROM site AS T1 INNER JOIN (SELECT site_id, SUM(view) AS view, SUM(conversion) AS conversion FROM site_view GROUP BY site_id HAVING SUM(conversion) > 90000) AS T2 USING(site_id) WHERE T1.site_name ~ '[0-5]$' ORDER BY site_id) AS S1 LIMIT 10 OFFSET 0; |
SQL2 | SELECT * FROM (SELECT site_id, site_name, view, conversion FROM site AS T1 INNER JOIN (SELECT site_id, SUM(view) AS view, SUM(conversion) AS conversion FROM site_view GROUP BY site_id) AS T2 USING(site_id) WHERE T1.site_name ~ '[0-5]$' AND T2.conversion > 90000 ORDER BY site_id) AS S1 LIMIT 10 OFFSET 0; |
HAVINGãå©ç¨ããSQLãSQL1ã§WHEREå¥ãå©ç¨ããã®ãSQL2ã§ãã
ä½æ¹ãã©ã£ã¡ãªæ°ãããªãã¯ãªããæ¤è¨¼
å®è¡ãã¡ã¤ã« | 1åç® | 2åç® | 3åç® | 4åç® | 5åç® | 6åç® | 7åç® | 8åç® | 9åç® | 10åç® | æå° | æ大 | å¹³å |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sql1.sql | 1180.76 | 1383.684 | 1326.127 | 1376.931 | 1359.841 | 1411.559 | 1368.94 | 1324.281 | 1383.121 | 1211.007 | 1180.76 | 1411.559 | 1332.625 |
sql2.sql | 1359.779 | 1306.331 | 1312.314 | 1350.094 | 1327.714 | 1377.518 | 1405.109 | 1362.456 | 1313.428 | 1146.364 | 1146.364 | 1405.109 | 1326.111 |
ä»®ã«PHPãJavaã§WHEREå¥ãHAVINGãåççæããã®ãªãSQL2ã®æ¹ããããããããªæ°ãããã®ã§SQL2ã«è»é ãã? ã¨ã¾〜é·ã ã¨æ¤è¨¼ãã¦ããã¾ãã。
çµè«ã¨ãã¦ã¯、GROUP BYãå ã«è¡ãJOINãã¾ãããã«éã
ä»åº¦ã¯、æå¥ã¬ãã¼ãã®ä½æã§æ¤è¨¼ãããã¨æãã¾ã。