Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize 5 table SQL query (stores => items => words)
    primarykey
    data
    text
    <blockquote> <h1>Tables</h1> <p><strong>stores (100,000 rows)</strong>: id (pk), name, lat, lng, ...</p> <p><strong>store_items (9,000,000 rows)</strong>: store_id (fk), item_id (fk)</p> <p><strong>items (200,000 rows)</strong>: id(pk), name, ...</p> <p><strong>item_words (1,000,000 rows)</strong>: item_id(fk), word_id(fk)</p> <p><strong>words (50,000 rows)</strong>: id(pk), word VARCHAR(255)</p> </blockquote> <p>Note: all ids are integers.</p> <p>========</p> <blockquote> <h1>Indexes</h1> <p>CREATE UNIQUE INDEX storeitems_storeid_itemid_i ON store_items(store_id,item_id);</p> <p>CREATE UNIQUE INDEX itemwords_wordid_itemid_i ON item_words(word_id,item_id);</p> <p>CREATE UNIQUE INDEX words_word_i ON words(word);</p> </blockquote> <p>Note: I prefer multi column indexes (storeitems_storeid_itemid_i and itemwords_wordid_itemid_i) because: <a href="http://www.mysqlperformanceblog.com/2008/08/22/multiple-column-index-vs-multiple-indexes/" rel="nofollow">http://www.mysqlperformanceblog.com/2008/08/22/multiple-column-index-vs-multiple-indexes/</a></p> <h1>QUERY</h1> <pre><code>select s.name, s.lat, s.lng, i.name from words w, item_words iw, items i, store_items si, stores s where iw.word_id=w.id and i.id=iw.item_id and si.item_id=i.id and s.id=si.store_id and w.word='MILK'; </code></pre> <h2>Problem: elapsed time is 20-120 sec (depending on the word)!!!</h2> <pre><code>explain $QUERY$ +----+-------------+-------+--------+-------------------------------------------------------+-----------------------------+---------+-----------------------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+-------------------------------------------------------+-----------------------------+---------+-----------------------------+------+-------------+ | 1 | SIMPLE | w | const | PRIMARY,words_word_i | words_word_i | 257 | const | 1 | Using index | | 1 | SIMPLE | iw | ref | itemwords_wordid_itemid_i,itemwords_itemid_fk | itemwords_wordid_itemid_i | 4 | const | 1 | Using index | | 1 | SIMPLE | i | eq_ref | PRIMARY | PRIMARY | 4 | iw.item_id | 1 | | | 1 | SIMPLE | si | ref | storeitems_storeid_itemid_i,storeitems_itemid_fk | storeitems_itemid_fk | 4 | iw.item_id | 16 | Using index | | 1 | SIMPLE | s | eq_ref | PRIMARY | PRIMARY | 4 | si.store_id | 1 | | </code></pre> <h2>I want elapsed time to be less than 5 secs!!! Any ideas???</h2> <p>==============</p> <h1>What I tried</h1> <p>I tried to see when increase in the execution time happens by adding tables to the query.</p> <h2>1 table</h2> <pre><code>select * from words where word='MILK'; Elapsed time: 0.4 sec </code></pre> <h2>2 tables</h2> <pre><code>select count(*) from words w, item_words iw where iw.word_id=w.id and w.word='MILK'; Elapsed time: 0.5-2 sec (depending on word) </code></pre> <h2>3 tables</h2> <pre><code>select count(*) from words w, item_words iw, items i where iw.word_id=w.id and i.id=iw.item_id and w.word='MILK'; Elapsed time: 0.5-2 sec (depending on word) </code></pre> <h2>4 tables</h2> <pre><code>select count(*) from words w, item_words iw, items i, store_items si where iw.word_id=w.id and i.id=iw.item_id and si.item_id=i.id and w.word='MILK'; Elapsed time: 20-120 sec (depending on word) </code></pre> <p>I guess the problem with the indexes or with the design of query/database. But there must be a way to make it work fast. Google does it somehow and their tables are much bigger!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload