Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql query performance is slow, need help in optimization
    primarykey
    data
    text
    <p>I have 4 tables: shipments (200K records), stores (45 records), product_stores (8K records), districts (698 records). The following query takes very long time to excute (12 seconds):</p> <pre><code>SELECT `s`. * , `p`.`productCode` , `p`.`productName` , `st`.`name` AS `storeName` , `d`.`name` AS `districtName` FROM `shipments` AS `s` JOIN `product_stores` AS `p` ON s.productStoreId = p.id JOIN `stores` AS `st` ON s.storeId = st.id LEFT JOIN `districts` AS `d` ON s.districtId = d.id WHERE (s.storeId IN (1, 2, 3, 4, 6, 9, 14, 16, 22, 26, 30)) GROUP BY `s`.`id` ORDER BY `s`.`id` DESC LIMIT 100 </code></pre> <p>EXPLAIN query return the following result:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE st ALL PRIMARY NULL NULL NULL 45 Using where; Using temporary; Using filesort 1 SIMPLE s ref fk_shipments_stores1_idx,fk_shipments_product_stor... fk_shipments_stores1_idx 4 st.id 482 1 SIMPLE p eq_ref PRIMARY PRIMARY 4 s.productStoreId 1 1 SIMPLE d eq_ref PRIMARY PRIMARY 4 s.districtId 1 </code></pre> <ol> <li>I don't know why mysql must Using temporary; Using filesort in this case</li> <li>Why mysql not start select from shipments? then join store. it start select from store then join shipments? I try to run the EXPLAIN and sometime mysql start select from table product_stores</li> <li>Please help me to optimize the table, indexes...to improve performance.</li> </ol> <p>(I'm using mysql 5.0.95)</p> <p>This is the table structures:</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