MySQL自增主鍵排序問題


今天遇到一個問題,mysql數據庫,在有些場景下,使用自增主鍵id排序+limit查詢時,會很慢。

場景1:表數據50W+,id倒序/升序,limit分頁,結果集<=10,耗時 0.9~1.4秒,不分頁耗時100~180毫秒.

備注:結果集數量越小,查詢速度越慢。

如下SQL

SELECT
    id,
    corp_id,
    org_id,
    bill_code,
    bill_maker,
    product_key_id,
    latest,
    reject_and_submit_status,
    audit_status,
    sku_id,
    enable_flag,
    special_num,
    special_period,
    special_flag,
    batchno_flag,
    medicine_flag,
    erp_product_type,
    zdyh_flag,
    creator,
    ts,
    modify_date,
    modifier,
    yn,
    common_name,
    product_zjm,
    product_name,
    medicine_type,
    product_type,
    dosage_form,
    dosageform_str,
    specification,
    unit,
    unit_str,
    storage_condition,
    bar_code,
    pzwh,
    pzwhqx,
    producer,
    production_place,
    expire_date,
    bzcpsms,
    zlzxbz,
    mah,
    yfyl,
    syz,
    buyer,
    sn_flag,
    sfylqx,
    input_vat,
    output_vat,
    vc_medicine_flag,
    new_specific_flag,
    cold_chain_flag,
    storage_mode,
    min_temperature,
    max_temperature,
    submit_date,
    storage_location_code,
    storage_location_name,
    proof_code,
    elqxzjyfl,
    drug_safety_product_id,
    drug_safety_standard_code,
    prescription_flag,
    store_property
FROM
    my_table
WHERE
    org_id=180
AND audit_status IN (3,-2)
AND yn = 1
AND (
        sku_id LIKE CONCAT("%",'100001727506',"%")
    OR  common_name LIKE CONCAT("%",'100001727506',"%")
    OR  product_zjm LIKE CONCAT("%",'100001727506',"%"))
ORDER BY id DESC 

場景2:表數據50W+,id倒序,limit分頁,結果集>=10000,耗時 30~50毫秒,不分頁耗時500~600毫秒.

場景2:表數據50W+,id升序,limit分頁,結果集>=10000,耗時 200~300毫秒,不分頁耗時500~600毫秒.

備注:結果集數量越大,查詢速度越快

SELECT
    id,
    corp_id,
    org_id,
    bill_code,
    bill_maker,
    product_key_id,
    latest,
    reject_and_submit_status,
    audit_status,
    sku_id,
    enable_flag,
    special_num,
    special_period,
    special_flag,
    batchno_flag,
    medicine_flag,
    erp_product_type,
    zdyh_flag,
    creator,
    ts,
    modify_date,
    modifier,
    yn,
    common_name,
    product_zjm,
    product_name,
    medicine_type,
    product_type,
    dosage_form,
    dosageform_str,
    specification,
    unit,
    unit_str,
    storage_condition,
    bar_code,
    pzwh,
    pzwhqx,
    producer,
    production_place,
    expire_date,
    bzcpsms,
    zlzxbz,
    mah,
    yfyl,
    syz,
    buyer,
    sn_flag,
    sfylqx,
    input_vat,
    output_vat,
    vc_medicine_flag,
    new_specific_flag,
    cold_chain_flag,
    storage_mode,
    min_temperature,
    max_temperature,
    submit_date,
    storage_location_code,
    storage_location_name,
    proof_code,
    elqxzjyfl,
    drug_safety_product_id,
    drug_safety_standard_code,
    prescription_flag,
    store_property
FROM
    my_table
WHERE
    org_id=180
AND audit_status IN (3,-2)
AND yn = 1
AND (
        sku_id LIKE CONCAT("%",'1',"%")
    OR  common_name LIKE CONCAT("%",'1',"%")
    OR  product_zjm LIKE CONCAT("%",'1',"%"))
ORDER BY id desc
limit 20

 

具體原因:未查明....

結論:不要使用id排序與limit一起使用,如果需要,請做足場景測試,有效利用,避免出現慢SQL


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM