Note that there are some explanatory texts on larger screens.

plurals
  1. POSlow MySQL Query on ~400.000 entries
    primarykey
    data
    text
    <p>I have the following query that is really slow (2.9 seg) :</p> <pre><code>SELECT post_id FROM ap_props LEFT JOIN ap_moneda ON ( ap_props.rela_moneda = ap_moneda.id_moneda ) LEFT JOIN wp_posts ON ( ap_props.post_id = wp_posts.id ) WHERE 1 = 1 AND wp_posts.post_status = "publish" AND rela_inmuebleoper = "2" AND rela_inmuebletipo = "1" AND (( approps_precio * Ifnull(moneda_valor, 0) &gt;= 2000 AND approps_precio * Ifnull(moneda_valor, 0) &lt;= 6000 )) AND rela_barrio IN ( 6, 23085, 23086, 23087, 7, 23088, 23089, 23090, 23091, 23092, 26, 23115, 23116, 23117, 23118, 23119, 23120, 32, 43, 23123, 23124, 23125 ) AND ( post_id IS NOT NULL ); 2.90808200 </code></pre> <p>The profiling shows :</p> <pre><code>+--------------------------------+----------+ | Status | Duration | +--------------------------------+----------+ | starting | 0.000132 | | checking query cache for query | 0.000135 | | Opening tables | 0.000023 | | System lock | 0.000009 | | Table lock | 0.000033 | | init | 0.000074 | | optimizing | 0.000030 | | statistics | 0.001989 | | preparing | 0.000028 | | executing | 0.000007 | | Sending data | 2.905463 | | end | 0.000015 | | query end | 0.000005 | | freeing items | 0.000055 | | storing result in query cache | 0.000013 | | logging slow query | 0.000009 | | logging slow query | 0.000055 | | cleaning up | 0.000007 | +--------------------------------+----------+ </code></pre> <p>and the explain :</p> <pre><code>+----+-------------+-----------+-------------+---------------------------------------------------------------------+-------------------------------------------+---------+---------------------------+-------+-------------------------------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+-------------+----------------------------------------------------------------------+-------------------------------------------+---------+---------------------------+-------+-------------------------------------------------------------------------+ | 1 | SIMPLE | ap_props | index_merge | idx_post_id,idx_relabarrio,idx_relainmuebleoper,idx_relainmuebletipo | idx_relainmuebleoper,idx_relainmuebletipo | 5,5 | NULL | 58114 | Using intersect(idx_relainmuebleoper,idx_relainmuebletipo); Using where | | 1 | SIMPLE | ap_moneda | ALL | NULL | NULL | NULL | NULL | 3 | Using where | | 1 | SIMPLE | wp_posts | eq_ref | PRIMARY | PRIMARY | 8 | metaprop.ap_props.post_id | 1 | Using where | +----+-------------+-----------+-------------+----------------------------------------------------------------------+-------------------------------------------+---------+---------------------------+-------+-------------------------------------------------------------------------+ </code></pre> <p>Any ideas on how to improve it? The ammount of entries are ~400.000 in total both in ap_props and wp-posts. ap_moneda only has 5 entries.</p> <p>I tried removing the IN clause but the following shows the same performance results :</p> <pre><code>SELECT post_id from ap_props left join ap_moneda on (ap_props.rela_moneda = ap_moneda.id_moneda) left join wp_posts on (ap_props.post_id = wp_posts.ID) where 1=1 AND wp_posts.post_status = "publish" AND rela_inmuebleoper = "2" AND rela_inmuebletipo = "1" AND ( ( approps_precio * ifnull(moneda_valor,0) &gt;= 2000 AND approps_precio * ifnull(moneda_valor,0) &lt;= 6000) ) AND (rela_barrio=6 OR rela_barrio=23085 OR rela_barrio=23086 OR rela_barrio=23087 OR rela_barrio=7 OR rela_barrio=23088 OR rela_barrio=23089 OR rela_barrio=23090 OR rela_barrio=23091 OR rela_barrio=23092 OR rela_barrio=26 OR rela_barrio=23115 OR rela_barrio=23116 OR rela_barrio=23117 OR rela_barrio=23118 OR rela_barrio=23119 OR rela_barrio=23120 OR rela_barrio=32 OR rela_barrio=43 OR rela_barrio=23123 OR rela_barrio=23124 OR rela_barrio=23125) AND (post_id IS NOT NULL); 2.91080400 </code></pre> <p>Thanks a lot for your help!</p> <p>Edit :</p> <p>The current indexes are :</p> <pre><code>+----------+------------+----------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +----------+------------+----------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+ | ap_props | 0 | PRIMARY | 1 | approps_origen | A | 10 | NULL | NULL | | BTREE | | | ap_props | 0 | PRIMARY | 2 | approps_id_aviso | A | 452098 | NULL | NULL | | BTREE | | | ap_props | 1 | idx_status | 1 | approps_status_db | A | 3 | NULL | NULL | YES | BTREE | | | ap_props | 1 | idx_fecha | 1 | approps_fecha | A | 64585 | NULL | NULL | YES | BTREE | | | ap_props | 1 | idx_post_id | 1 | post_id | A | 452098 | NULL | NULL | YES | BTREE | | | ap_props | 1 | idx_relabarrio | 1 | rela_barrio | A | 2457 | NULL | NULL | YES | BTREE | | | ap_props | 1 | idx_relainmuebleoper | 1 | rela_inmuebleoper | A | 6 | NULL | NULL | YES | BTREE | | | ap_props | 1 | idx_relainmuebletipo | 1 | rela_inmuebletipo | A | 17 | NULL | NULL | YES | BTREE | | +----------+------------+----------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+ </code></pre> <p>FYI F fixed it by adding a new index idx_approps_precio and forcing both by adding "use index (idx_relabarrio,idx_approps_precio)"</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.
    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