mysql - 如何优化包含相同“in”语句的sql?
问题描述
select *
from t_link a
WHERE a.STAR_CITY_ID in (select cityId from `t_map_city` t
WHERE t.PROVINCEID = 350) and
a.END_CITY_ID in (select cityId from `t_map_city` t
WHERE t.PROVINCEID = 350);
这是我的sql。如何优化它以提高效率?
select * from `t_ne$link` a
JOIN (select cityId from `t_map_city` t WHERE t.PROVINCEID=350) tb1
on (a.END_CITY_ID in (tb1.cityId) and a.STAR_CITY_ID in (tb1.cityId));
我已将其更改为此,但它不起作用。
解决方案
好吧,使用EXISTS
也可能会有所帮助:
select *
from t_link a
WHERE exists(select 1 from `t_map_city`
where cityId = a.start_city_id and provinceId = 350)
and exists(select 1 from `t_map_city`
where cityId = a.end_city_id and provinceId = 350)
推荐阅读
- javascript - array.map 中的异步查询:ExpressJs 中的 Rest API
- python - 全局定义的线程局部属性在线程中不可访问
- php - 将 WooCommerce 购物车数量替换为可预订产品的人数
- android - 反应原生android:TypeError:网络请求失败
- neural-network - 预测数组为空 Mask RCNN
- python - Python Uvicorn - 获取 SSL 证书信息
- c++ - CMake target_include_directories 找不到私有标头的#include
- javascript - Javascript:避免急切的异步执行并优先考虑事件循环上的调用
- google-people-api - Google people api,如何访问字段 FileAS
- hadoop - 如何避免 Hive 交叉产品?