Questa query SQL mi fa schifo. Non l'ho scritto, ma è una massiccia causa di problemi sui nostri server. Sono disposto a dividerlo in più query e ad eseguire parte dell'elaborazione tramite PHP (ad esempio, il RAND()).Ottimizza questa query SQL
$sql = "SELECT a.code, a.ad_id, a.position, a.type, a.image, a.url, a.height, a.width
FROM " . AD_TABLE ." a, " . USER_GROUP_TABLE . " g
WHERE (a.max_views >= a.views OR a.max_views = '0')
AND (FIND_IN_SET(" .$forum_id. ", a.show_forums) > 0 OR a.show_all_forums = '1')
AND g.user_id = " . $user->data['user_id'] . "
AND FIND_IN_SET(g.group_id, a.groups)
AND FIND_IN_SET(" . $user->data['user_rank'] . ", a.ranks)
AND a.start_time < " . time() . "
AND a.end_time > " . time() . "
AND (a.clicks <= a.max_clicks OR a.max_clicks = '0')
ORDER BY rand()";
Yeesh, mi sento icky dopo aver incollato che ...
EDIT: Di seguito i risultati del "spiegare" in una query di esempio nel formato di cui sopra, separati da virgole:
"id","select_type","table","type","possible_keys","key","key_len","ref","rows","Extra"
1,"SIMPLE","g","ref","user_id","user_id","3","const",6,"Using temporary; Using filesort"
1,"SIMPLE","a","ALL","max_views","","","",10,"Using where"
È
Stai facendo una domanda o stai cercando di ottenere qualche SQL ottimizzato senza pagare per questo? – Mathew
Ti dispiacerebbe fare una SELEZIONA SCENA e pubblicare i risultati? – alexn
Disgust è il motore della correzione. – RedFilter