Note that there are some explanatory texts on larger screens.

plurals
  1. POinscrutable response of explain and how to decide to add an index in mysql
    primarykey
    data
    text
    <p>we have a very big database and to ask the question in very simple terms, I am unable to decide whether should I add an index on date field or not.</p> <p>My query is: should I add an index on date field for table A, which is very large and date (format: 2013-02-26 18:52:23). Following is my query:</p> <pre><code> SELECT As.id FROM As INNER JOIN A_items ON A_items.A_id = As.id AND A_items.type IN ('BilledItem', 'CustomerItem') WHERE (As.A_date BETWEEN '2013-01-15 18:52:23' AND '2013-01-30 18:52:23') AND A_items.category_id in ('20219') and A_items.product_id IN ('ACCDYHGYUDZNY7FZ') </code></pre> <p>now when I use explain on this it gives me following result:-</p> <pre><code> *************************** 1. row *************************** id: 1 select_type: SIMPLE table: A_items type: ref possible_keys: index_A_items_on_A_id,index_A_items_on_product_id,i_type_parent_id_item_type key: index_A_items_on_product_id key_len: 258 ref: const rows: 221122 Extra: Using where *************************** 2. row *************************** id: 1 select_type: SIMPLE table: As type: eq_ref possible_keys: PRIMARY key: PRIMARY key_len: 8 ref: database.A_items.A_id rows: 1 Extra: Using where 2 rows in set (0.00 sec) </code></pre> <p>However, when I remove A_items.product_id IN ('ACCDYHGYUDZNY7FZ') from my query and run this:-</p> <pre><code> explain SELECT As.id FROM As INNER JOIN A_items ON A_items.A_id = As.id AND A_items.type IN ('BilledItem', 'CustomerItem') WHERE (As.A_date BETWEEN '2013-01-15' AND '2013-01-30') AND A_items.category_id in ('2005') </code></pre> <p>I get :- </p> <pre><code> *************************** 1. row *************************** id: 1 select_type: SIMPLE table: As type: ALL possible_keys: PRIMARY key: NULL key_len: NULL ref: NULL rows: 15427520 &lt;--Notice this big number Extra: Using where *************************** 2. row *************************** id: 1 select_type: SIMPLE table: A_items type: ref possible_keys: index_A_items_on_A_id,i_type_parent_id_item_type key: index_A_items_on_A_id key_len: 8 ref: database.As.id rows: 1 Extra: Using where 2 rows in set (0.00 sec) </code></pre> <p>My question is why this big number of rows are not shown in the first query and how to decide whether to add index on date field or not???</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