sql优化经历

radial_position_info  表的数据大概在10万条

第一条sql查询耗时1.8s

第二条sql耗时  45ms

原sql:

SELECT
    IFNULL( rb.CITY, '未知' ) CITY,
    COUNT( DISTINCT rp.radial_code ) radialNetworkCount 
FROM
    radial_position_info rp
    LEFT JOIN radial_base_info rb ON rb.`CODE` = rp.radial_code 
GROUP BY
    rb.CITY


 优化后的sql:

SELECT
    rb.CITY,
    COUNT(temp.radial_code)  
FROM
    ( SELECT DISTINCT rp.radial_code FROM radial_position_info rp ) temp
    LEFT JOIN radial_base_info rb ON rb.`CODE` = temp.radial_code
    GROUP BY rb.CITY

你可能感兴趣的:(sql优化经历)