MySQL社区

标题: 如何对以下多表查询的优化 [打印本页]

作者: milo    时间: 2013-3-14 13:52
标题: 如何对以下多表查询的优化
  1. select count(*)
  2. from tbl_alarmlog a
  3. where ((a.dev_type=75)
  4. or ((select id from tbl_userdev where a.dev_id=dev_id and a.dev_type=dev_type and user_id=4) is not null)
  5. or (a.dev_type=4 and (select id from tbl_userdev where dev_type=9 and user_id=4 and dev_id in (select a_id from tbl_point where id=a.dev_id)) is not null)
  6. or (a.dev_type=7 and a.serverid=0 and (select id from tbl_patroluser where a.dev_id=ep_id and userid=4 or userid in (select id from tbl_user where creater_id=4)) is not null)
  7. or (a.dev_type=7 and a.serverid<>0)
  8. or ((a.dev_type=1 or a.dev_type=128) and (select id from tbl_userdev where user_id=4 and dev_type=64 and dev_no=a.puid and dev_id=a.channel) is not null))
  9. and a.alarm_time>=FROM_UNIXTIME(1330531200) and a.alarm_time<FROM_UNIXTIME(1363190399) and a.serverid=0
复制代码
请问如何把上述的or语句改成and,貌似说查询子句中or语句会导致全表搜索。
或者说如何优化上述语句,哪些地方要加索引,哪些语句可以优化的?




欢迎光临 MySQL社区 (http://mysqlpub.com/) Powered by Discuz! X3.2