duane2364 2014-05-20 07:32
浏览 136
已采纳

Mysql查询大数据表的执行时间优化

I am executing a mysql query for searching car information from a table having 530399 records

for Executing query it is taking so much time

SELECT c.* FROM CarInfo as c WHERE (c.Vehicle_Year<='2014') and c.Vehicle_Age_Type='USED' limit 0,15 . 

I need all the fields from table so using * .

My table have 36 columns . Is there any way to optimize this query .

After adding index it is loading fast with limit but its taking time when trying to use total count

SELECT count(*) as total FROM CarInfo as c WHERE (c.Vehicle_Year<='2014') and c.Vehicle_Dealer_Zip in(85320,85354,85541) and (c.Vehicle_age_type='New' or c.Vehicle_age_type='Used' or c.Vehicle_age_type='Certified Used')

Dealer_Zip may contain so may values.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doumen1883 2014-05-20 07:39
    关注

    Looks like your table is missing the indexes and if yes you need to add them first.

    Before adding the index first check if its already there using the following command

    show indexes from CarInfo
    

    From the above command see if Vehicle_Year and Vehicle_Age_Type is having index and since you mentioned the query needs optimization I guess you are missing the indexes.

    Next step add the index as

    alter table CarInfo add index type_year_idx (Vehicle_Age_Type,Vehicle_Year);
    

    NOTE : You must take a backup of the table before adding the index

    Then re-frame the query as

    SELECT c.* 
    FROM CarInfo as c 
    WHERE c.Vehicle_Age_Type='USED' 
    AND c.Vehicle_Year<='2014' limit 0,15 ;
    

    In addition when you feel the query is taking long time you should always use EXPLAIN to see what this query is up to so you can plan for the optimization. The syntax looks like below for your current query.

    EXPLAIN  
     SELECT c.* 
        FROM CarInfo as c 
        WHERE 
        c.Vehicle_Year<='2014'
        AND c.Vehicle_Age_Type='USED' 
    limit 0,15 ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容