Upgrade to Pro — share decks privately, control downloads, hide ads and more …

実行計画の話

 実行計画の話

第14回 中国地方DB勉強会 in 福山の登壇資料です。
https://dbstudychugoku.github.io/events/event-014.html

合わせてこちらのYouTube動画を見ることをオススメします。

PGCon 2014 Tokyo【D3】PostgreSQL SQL チューニング入門 入門編(下雅意 美紀)

https://www.youtube.com/watch?v=gxsBi-6ub3k&nohtml5=False

soudai sone

April 14, 2016
Tweet

More Decks by soudai sone

Other Decks in Technology

Transcript

  1. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html
  2. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾJETFMFDU@UZQF JE͸ΫΤϦͷॱ൪Λද͢ʢ+0*/ͷ࣌ʹॏཁʣ TFMFDU@UZQF͸ΫΤϦͷछྨΛද͢ʢαϒΫΤϦͷ࣌ʹॏཁʣ ԼهҎ֎ʹ΋͋Δ ɾ+0*/ͷTFMFDU@UZQF 4*.1-& /FTUFE-PPL+PJO͔͠ͳ͍ʣ ɾαϒΫΤϦͷTFMFDU@UZQF 13*."3:ɾɾɾ֎෦ΫΤϦΛࣔ͢ɻ 46#26&3:ɾɾɾ૬ؔؔ܎ͷͳ͍αϒΫΤϦɻ %&1&/%&/546#26&3:ɾɾɾ૬ؔؔ܎ͷ͋ΔαϒΫΤϦɻ 6/$"$)&"#-&46#26&3:ɾɾɾ࣮ߦ͢Δ౓ʹ݁Ռ͕มΘΔՄೳੑͷ͋ΔαϒΫΤϦɻ %&3*7&%ɾɾɾ'30.۟Ͱ༻͍ΒΕ͍ͯΔαϒΫΤϦɻ ."5&3*"-*;&%ɾɾɾ࣮ମԽ͞ΕͨαϒΫΤϦ
  3. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾJETFMFDU@UZQF JE͸ΫΤϦͷॱ൪Λද͢ʢ+0*/ͷ࣌ʹॏཁʣ TFMFDU@UZQF͸ΫΤϦͷछྨΛද͢ʢαϒΫΤϦͷ࣌ʹॏཁʣ ԼهҎ֎ʹ΋͋Δ ɾ+0*/ͷTFMFDU@UZQF 4*.1-& /FTUFE-PPL+PJO͔͠ͳ͍ʣ ɾαϒΫΤϦͷTFMFDU@UZQF 13*."3:ɾɾɾ֎෦ΫΤϦΛࣔ͢ɻ 46#26&3:ɾɾɾ૬ؔؔ܎ͷͳ͍αϒΫΤϦɻ %&1&/%&/546#26&3:ɾɾɾ૬ؔؔ܎ͷ͋ΔαϒΫΤϦɻ 6/$"$)&"#-&46#26&3:ɾɾɾ࣮ߦ͢Δ౓ʹ݁Ռ͕มΘΔՄೳੑͷ͋ΔαϒΫΤϦɻ %&3*7&%ɾɾɾ'30.۟Ͱ༻͍ΒΕ͍ͯΔαϒΫΤϦɻ ."5&3*"-*;&%ɾɾɾ࣮ମԽ͞ΕͨαϒΫΤϦ ߦ͝ͱʹ࣮ߦ͞ΕΔαϒΫΤϦ ͭ·Γܶతʹ஗͍
  4. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾUBCMF ΞΫηε͢Δର৅ͷςʔϒϧ
  5. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾUZQF ϨίʔυΞΫηελΠϓɻ ཁ͸*/%&9ͷར༻ͷ༗ແ΍࢖͍ํΛදͨ͠΋ͷ ɾओͳछྨ DPOTUɾɾɾ13*."3:,&:·ͨ͸6/*26&ΠϯσοΫεͷϧοΫΞοϓʹΑΔΞΫηεɻ࠷଎ɻ FR@SFGɾɾɾ+0*/ʹ͓͍ͯ13*"3:,&:·ͨ͸6/*26&,&:͕ར༻͞ΕΔ࣌ͷΞΫηελΠϓɻDPOTUͱࣅ͍ͯΔ͕+0*/ Ͱ༻͍ΒΕΔͱ͜Ζ͕ҧ͏ɻ SFGɾɾɾϢχʔΫʢ13*."3:PS6/*26&ʣͰͳ͍ΠϯσοΫεΛ࢖ͬͯ౳Ձݕࡧʢ8)&3&LFZWBMVFʣΛߦͬͨ ࣌ʹ࢖ΘΕΔΞΫηελΠϓɻ SBOHFɾɾɾΠϯσοΫεΛ༻͍ͨൣғݕࡧɻ JOEFYɾɾɾϑϧΠϯσοΫεεΩϟϯɻΠϯσοΫεશମΛεΩϟϯ͢Δඞཁ͕͋ΔͷͰͱͯ΋஗͍ɻ "--ɾɾɾϑϧςʔϒϧεΩϟϯɻΠϯσοΫε͕·ͬͨ͘ར༻͞Ε͍ͯͳ͍͜ͱΛࣔ͢ɻ0-51ܥͷॲཧͰ͸վળඞਢɻ
  6. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾUZQF ϨίʔυΞΫηελΠϓɻ ཁ͸*/%&9ͷར༻ͷ༗ແ΍࢖͍ํΛදͨ͠΋ͷ ɾओͳछྨ DPOTUɾɾɾ13*."3:,&:·ͨ͸6/*26&ΠϯσοΫεͷϧοΫΞοϓʹΑΔΞΫηεɻ࠷଎ɻ FR@SFGɾɾɾ+0*/ʹ͓͍ͯ13*"3:,&:·ͨ͸6/*26&,&:͕ར༻͞ΕΔ࣌ͷΞΫηελΠϓɻDPOTUͱࣅ͍ͯΔ͕+0*/ Ͱ༻͍ΒΕΔͱ͜Ζ͕ҧ͏ɻ SFGɾɾɾϢχʔΫʢ13*."3:PS6/*26&ʣͰͳ͍ΠϯσοΫεΛ࢖ͬͯ౳Ձݕࡧʢ8)&3&LFZWBMVFʣΛߦͬͨ ࣌ʹ࢖ΘΕΔΞΫηελΠϓɻ SBOHFɾɾɾΠϯσοΫεΛ༻͍ͨൣғݕࡧɻ JOEFYɾɾɾϑϧΠϯσοΫεεΩϟϯɻΠϯσοΫεશମΛεΩϟϯ͢Δඞཁ͕͋ΔͷͰͱͯ΋஗͍ɻ "--ɾɾɾϑϧςʔϒϧεΩϟϯɻΠϯσοΫε͕·ͬͨ͘ར༻͞Ε͍ͯͳ͍͜ͱΛࣔ͢ɻ0-51ܥͷॲཧͰ͸վળඞਢɻ ݟ͚ͭͨΒνϡʔχϯάର৅ʂ
  7. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾQPTTJCMF@LFZT ΦϓςΟϚΠβ͕ςʔϒϧͷΞΫηεʹར༻Մೳ ͳΠϯσοΫεͷީิͱͯ͠ڍ͛ͨΩʔͷҰཡ
  8. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾLFZ ΦϓςΟϚΠβʹΑͬͯબ୒͞ΕͨΩʔ
  9. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾLFZ@MFO બ୒͞ΕͨΩʔͷ௕͞ ΠϯσοΫεͷ૸ࠪ͸ɺΩʔ௕͕୹͍ํ͕ߴ଎
  10. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾSFG ݕࡧ৚݅ͰɺLFZͱൺֱ͞Ε͍ͯΔ஋΍ΧϥϜͷछྨ ఆ਺͕ࢦఆ͞Ε͍ͯΔ৔߹͸DPOTUͱදࣔ͞ΕΔ +0*/͕࣮ߦ͞Ε͍ͯΔ࣌ʹ͸ɺ݁߹͢Δ૬खଆͷςʔϒϧͰݕࡧ৚݅ͱͯ͠ར༻͞Ε͍ͯΔΧϥϜ͕දࣔ͞ΕΔ
  11. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾSFG ݕࡧ৚݅ͰɺLFZͱൺֱ͞Ε͍ͯΔ஋΍ΧϥϜͷछྨ ఆ਺͕ࢦఆ͞Ε͍ͯΔ৔߹͸DPOTUͱදࣔ͞ΕΔ +0*/͕࣮ߦ͞Ε͍ͯΔ࣌ʹ͸ɺ݁߹͢Δ૬खଆͷςʔϒϧͰݕࡧ৚݅ͱͯ͠ར༻͞Ε͍ͯΔΧϥϜ͕දࣔ͞ΕΔ $PVOUSZςʔϒϧ͸$JUZςʔϒϧͱ$JUZ$PVOUSZ$PEF ΧϥϜͰ+0*/͞ΕΔͱ͍͏͜ͱΛ͍ࣔͯ͠Δ
  12. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾSPXT ͦͷςʔϒϧ͔ΒϑΣον͞ΕΔߦ਺ͷݟੵ΋Γ ͜ͷϑΟʔϧυ͸͋͘·Ͱ΋ςʔϒϧશମͷߦ਺΍ΠϯσοΫεͷ෼ࢄ۩߹͔Βಋ͖ग़͞Εͨେ·͔ͳݟੵ΋Γͳ ͷͰɺ࣮ࡍʹϑΣον͞ΕΔਖ਼֬ͳߦ਺Ͱ͸ͳ͍ ͔͠͠ྫ֎ͱͯ͠%&3*7&%ςʔϒϧ͸࣮ࡍʹ࣮ߦ͢ΔͷͰਖ਼֬ͳݟੵ΋ΓʹͳΔ ඇৗʹ஗͍αϒΫΤϦͷ৔߹ɺ&91-"*/Ͱ΋͕͔͔࣌ؒΔՄೳੑ͕ߴ͍ ޙड़͢Δ6TJOHXIFSF͕&YUSBϑΟʔϧυʹදࣔ͞Ε͍ͯΔ৔߹͸ɺϑΣονͨ͠ߦʹରͯ͠͞Βʹ8)&3&۟ͷݕ ࡧ৚͕݅ద༻͞ΕͯߦͷߜΓࠐΈ͕ߦΘΕΔͷͰɺΫϥΠΞϯτ΁ฦ͞ΕΔ݁Ռߦ͸গͳ͘ͳΔՄೳੑ͕͋Δ +0*/ͷࡍͷॏཁͳࢦඪʹͳΔ
  13. ࣮ߦܭը mysql> EXPLAIN SELECT * FROM Country, (SELECT * FROM

    City WHERE Population > 1000000) AS C1 WHERE Country.Code = C1.CountryCode; +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 237 | | | 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | C1.CountryCode | 1 | | | 2 | DERIVED | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+------------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) Ҿ༻ݩ:MySQLͷEXPLAINΛపఈղઆ!! http://nippondanji.blogspot.jp/2009/03/mysqlexplain.html ɾ&YUSB ͦͷΫΤϦΛ࣮ߦ͢ΔͨΊʹΦϓςΟϚΠβ͕ͲͷΑ͏ͳઓུΛબ୒͔ͨ͠ͱ͍͏͜ͱΛࣔ͢ ɾओͳछྨʢಉ࣌ʹෳ਺දࣔ͞ΕΔ৔߹͕͋Δʣ 6TJOHXIFSFɾɾɾසൟʹग़ྗ͞ΕΔ௥Ճ৘ใͰ͋Δɻ8)&3&۟ʹݕࡧ৚͕݅ࢦఆ͞Ε͓ͯΓɺͳ͓͔ͭΠϯσο ΫεΛݟ͚ͨͩͰ͸8)&3&۟ͷ৚݅Λશͯద༻͢Δ͜ͱ͕ग़དྷͳ͍৔߹ʹදࣔ͞ΕΔɻ 6TJOHJOEFYɾɾɾΫΤϦ͕ΠϯσοΫε͚ͩΛ༻͍ͯղܾͰ͖Δ͜ͱΛࣔ͢ɻ$PWFSJOH*OEFYΛར༻͍ͯ͠Δ৔ ߹ͳͲʹදࣔ͞ΕΔɻ 6TJOHpMFTPSUɾɾɾpMFTPSUʢΫΠοΫιʔτʣͰιʔτΛߦ͍ͬͯΔ͜ͱΛࣔ͢ɻ6TJOHpMFTPSUʹ͍ͭͯ͸ઌ೔ ৄ͘͠આ໌ͨ͠ͷͰࢀর͞Ε͍ͨɻ 6TJOHUFNQPSBSZɾɾɾ+0*/ͷ݁ՌΛιʔτͨ͠Γɺ%*45*/$5ʹΑΔॏෳͷഉআΛߦ͏৔߹ͳͲɺΫΤϦͷ࣮ߦ ʹςϯϙϥϦςʔϒϧ͕ඞཁͳ͜ͱΛࣔ͢ɻ 6TJOHJOEFYGPSHSPVQCZɾɾɾ.*/ ."9 ͕(3061#:۟ͱซ༻͞Ε͍ͯΔͱ͖ɺΫΤϦ͕ΠϯσοΫεͩ +0*/ͷࡍͷॏཁͳࢦඪʹͳΔ
  14. ࣮ߦܭը CREATE TABLE `demo`.`users` ( `id` INT NOT NULL AUTO_INCREMENT

    COMMENT '', `name` VARCHAR(45) NOT NULL COMMENT '', `age` INT NOT NULL COMMENT '', `created` DATETIME NOT NULL DEFAULT NOW () COMMENT '', PRIMARY KEY (`id`) COMMENT '' ); ——શ݅Λબ୒͢ΔͷͰςʔϒϧεΩϟϯ SELECT * FROM demo.users; ——PRIMARY KEYʢINDEXʣΛར༻ͨ͠ྫ SELECT * FROM demo.users WHERE id > 100; ——INDEX͕ແ͍ͷͰςʔϒϧεΩϟϯ SELECT * FROM demo.users WHERE age > 20;
  15. αϒΫΤϦ —— INDEXͱͯ͠PRIMARY KEY͕ޮ͍͍ͯΔ SELECT * FROM users WHERE id

    < 1000 AND id > 800 —— 1ճ͔࣮͠ߦ͞Εͳ͍ SELECT * FROM (SELECT * FROM users WHERE id < 1000 AND id > 800) AS dummy
  16. JOIN SELECT * FROM users INNER JOIN users AS tmp

    ON tmp.id = users.id AND tmp.id BETWEEN 10 AND 100000 WHERE users.created = '2016-02-27 04:31:32'
  17. ෳ਺ར༻ͨ͠INDEX CREATE TABLE public.users ( id integer NOT NULL DEFAULT

    nextval('users_id_seq'::regclass), name text NOT NULL, age integer NOT NULL, created timestamp without time zone NOT NULL DEFAULT now(), CONSTRAINT users_pkey PRIMARY KEY (id)); CREATE INDEX users_created_idx ON public.users USING tree (created); ——idͱcreatedͷINDEXΛར༻͢Δ SELECT * FROM users WHERE id < 100 AND created < '2016-02-27 05:41:28';
  18. ૬ؔαϒΫΤϦ SELECT * FROM users WHERE age IN (SELECT age

    FROM users WHERE id BETWEEN 10 AND 100000)
  19. ΢Πϯυ΢ؔ਺ SELECT rank() OVER (PARTITION BY age ORDER BY id)

    , * FROM users WHERE age BETWEEN 10 AND 30 LIMIT 100