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 ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数