performance - MySQL - basic 2 table query slow - where, index? -


i have mysql 5.0 query regularly taking 14+ seconds, called web page, , users impatient. it's simple, selecting 11 columns 2 tables. have 3 questions:

  1. does placement of join matter?
  2. does order of clause matter, or mysql optimize?
  3. would , index in case?

sql:

select table1.id, table1.dateopened, table1.status, table2.name, etc (table1 join table2 on((table1.currentname = table2.id)))  table1.type = 'add' , (status = 'open' or status = 'pending'); 

table/column info:

table1 has 750,000 rows, table2 1.5m rows. indexed: table1.id, table2.id int columns: id, table1.currentname table1.status = populated 1 of 4 values,                  maybe 300 'open' or 'pending' table1.type = 3 possible values: 'add', 'change', or null 
  1. is there advantage joining in from, vs adding 'table1.currentname = table2.id' in clause?

  2. there 3 clauses (with join). ran explain various order combinations, , results seemed same.

  3. i thought adding index table1.currentname may help, i'm thinking not. modified query remove references table2, , still ran slow. (see 3b)

  4. seems bulk of slowdown may reading 800k records looking @ type , status values. make sense index these 2 columns, there 3 or 4 possible values? thought made sense when there more unique values.

explain results:

+----+-------------+--------+--------+---------------+---------+---------+-----------------------+--------+-------------+  | id | select_type | table  | type   | possible_keys | key     | key_len | ref                   | rows   |       |          +----+-------------+--------+--------+---------------+---------+---------+-----------------------+--------+-------------+  |  1 | simple      | table1 |    | currentname   | null    | null    | null                  | 733190 | using |  |  1 | simple      | table2 | eq_ref | primary       | primary | 4       | db.table1.currentname | 1      |             |  +----+-------------+--------+--------+---------------+---------+---------+-----------------------+--------+-------------+  2 rows in set (0.00 sec) 

does placement of join matter?

the order written not matter inner joins.

does order of clause matter, or mysql optimize?

no. written order in clause not matter mysql query parser , optimizer

would , index in case?

potentially. compound index type_status (type, status) on table1 since clause reduce initial rows read.

is there advantage joining in from, vs adding 'table1.currentname = table2.id' in clause?

for inner joins, doesn't matter if join condition in clause or clause.

i thought adding index table1.currentname may help, i'm thinking not. modified query remove references table2, , still ran slow. (see 3b)

an index on table1.currentname not query.

seems bulk of slowdown may reading 800k records looking @ type , status values.

this reinforces thought above. add compound index (potentially not thing online), it'd like

alter table table1 add index type_status (type, status); 

i thought made sense when there more unique values.

selectivity helps, high cardinality not suitable context.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -