优化前语句
SELECT * FROM a LEFT JOIN b ON a.id=b.a_id ORDER a.id DESC
优化后语句
SELECT * FROM a LEFT JOIN b ON a.id=b.a_id JOIN (SELECT id FROM a ORDER BY id DESC) a_order ON a.id = a_order.id
实际工作中100W+的几个表使用LEFT JOIN要20分钟才能得到结果,语句优化后3秒。
以下是实际操作中Durpal Views的 hook_views_query_alter() 使用代码
$views_join = new views_join();
$views_join->extra_type = 'AND';
$views_join->table = '(SELECT aid FROM activity_log ORDER BY aid DESC)';
$views_join->left_table = 'activity_log';
$views_join->left_field = 'aid';
$views_join->field = 'aid';
$views_join->type = 'INNER';
$views_join->adjusted = true;
$query->table_queue['activity_log_order']['join'] = $views_join;
$query->table_queue['activity_log_order']['alias'] = 'activity_log_order';?>
没有评论:
发表评论