简体   繁体   中英

How to reduce memory and cpu usage for mysql query?

I have more 300,000 rows in my table.I want to remove duplicate row and same data. So I run these query.

$sql=mysql_query("SELECT DISTINCT n1.id FROM dv2xp_adsmanager_ads n1, dv2xp_adsmanager_ads n2 WHERE n1.id < n2.id AND n1.ad_imgone = n2.ad_imgone AND n1.ad_text = n2.ad_text  LIMIT $start,$limit ");

These query can show duplicated row.But this is very slow and increased cup usage to 100% on my server. How to resolve these solution?

Without knowing much about the data you're using, my best guess is that you could work with INNER and OUTER JOIN. DISTINCT will bring distinctive results based on the main key, but if you have multiple tables with multiple keys, the results might not be optimal. For that reason, it is better to use the JOINs in order to bring just the essential data from each table.

This is a nice article about JOINs: What is the difference between “INNER JOIN” and “OUTER JOIN”?

I hope this information helps and puts you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM