postgresã§SQLã®è¨è¿°ã§é«éåãå³ãã¾ã。
ãã©ã¡ã¼ã¿èª¿æ´ã§å¤ã
ããã¿ããã§ããå¹çã®è¯ãSQLã«ã¤ãã¦æ¤è¨¼ãã¦ã¿ã¾ã。
ååã®ãã¹ããã¼ãã«
postgresã§é£çªã®ãã¹ããã¼ã¿ãä½æã®ãã¼ã¿ããã¨ã«ãã¾ã。
æ¤è¨¼ããæ¡ä»¶ã¯ä»¥ä¸ã§ã。
1.æãã¤ããã¿ã¼ã³ãè¤æ°SQLãè¨è¼.
2.æ¤è¨¼ã«ã¯10åã®ãã¼ã¿ãç¨ãã¾ã.
3.\timingã®å¤ãæ¤è¨¼çµæã¨ãã¾ã.
4.limit offsetãå©ç¨åæ°æ¯ã«ç§»åããã¾ã.
5.WHEREå¥ã®æç¡ã§ã®å¤åã確èª.
ä¸è¨æ¡ä»¶ã主ãªãªãããã«ãªãã¾ã。
site,site_viewãã¡ãã®ï¼ãã¼ãã«ã®æ¤ç´¢ã§ã。
\timingã¨å¼ã°ããã³ãã³ããå©ç¨ãããã¨ã§ç°¡åã«å®è¡æéã
è¨ããã¨ãã§ããã®ã§ãã¡ããå©ç¨.
æ¤è¨¼1
ãµã¤ãæ¯ã®åè¨viewæ°,conversionæ°ãè¨ç®
SQL1 |
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) GROUP BY site_id, site_name ORDER BY site_id LIMIT 10 OFFSET 0; |
SQL2 |
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) ORDER BY site_id LIMIT 10 OFFSET 0; |
ä¸è¨ã®SQL1,SQL2ãæ¤è¨¼ãã¾ã.
æ¤è¨¼ç¨ã®ã·ã§ã«ãä½æ.
sql1.sql
\timing
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) GROUP BY site_id, site_name ORDER BY site_id LIMIT 10 OFFSET 0;
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) GROUP BY site_id, site_name ORDER BY site_id LIMIT 10 OFFSET 10;
・・・
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) GROUP BY site_id, site_name ORDER BY site_id LIMIT 10 OFFSET 90;
sql2.sql
\timing
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) ORDER BY site_id LIMIT 10 OFFSET 0;
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) ORDER BY site_id LIMIT 10 OFFSET 10;
・・・
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) ORDER BY site_id LIMIT 10 OFFSET 90;
ããããã®æéã以ä¸ã®ã³ãã³ãã§æ¤è¨¼ãã¾ã.
psql -U postgres -f sql1.sql analytics | grep Time: | cut -d " " -f 2
psql -U postgres -f sql2.sql analytics | grep Time: | cut -d " " -f 2
以ä¸ãæ¤è¨¼çµæã§ã。
å®è¡ãã¡ã¤ã« |
1åç® |
2åç® |
3åç® |
4åç® |
5åç® |
6åç® |
7åç® |
8åç® |
9åç® |
10åç® |
æå° |
æ大 |
å¹³å |
sql1.sql |
2314.365 |
2247.267 |
2331.978 |
2235.310 |
2244.249 |
2457.474 |
2531.733 |
2445.966 |
2614.957 |
2483.380 |
2235.31 |
2614.957 |
2390.6679 |
sql2.sql |
1080.565 |
1041.092 |
1062.615 |
1019.743 |
1037.495 |
1031.203 |
1020.304 |
1049.538 |
1021.847 |
1043.459 |
1019.743 |
1080.565 |
1040.7861 |
ã¨æ¤è¨¼çµæã¯ãã®ããã«ãªãã¾ãã。
sql2ã®ã»ããå§åçã§ã。(ããã¾ã§ã§ãã®ãw
ããããããã«WHEREå¥ãä»ãããæ¤è¨¼ãã¦ããããã¨æãã¾ã。
ã¨ãããããã®è¨äºã¯ããã¾ã§æ¬¡åpart2ãè¨äºåãã¦ããã¾ã。