不会sql语句....谁帮我优化下

2019-03-25 13:45|来源: 网路

select * from dbo.tbGoodsInfo where 
(
id in (select goodsId from dbo.tbGoodsAddLink ) 
or id in(
select goodsId from dbo.tbGoodsBindBar  where  barUserId=
(select id from  dbo.t_ClientInfo where bh='cdcs')
)) 
and auditStatus=0 and bupd!=-1
union
select * from dbo.tbGoodsInfo where (id not in (select goodsId from dbo.tbGoodsAddLink ) 
and id  not in(select goodsId from dbo.tbGoodsBindBar  where  
barUserId=(select id from  dbo.t_ClientInfo where bh='cdcs')
)) and auditStatus=0 and bupd!=-1

谢谢

问题补充:dbo.tbGoodsInfo  商品表   dbo.tbGoodsAddLink  商品绑定地区表
dbo.tbGoodsBindBar 商品绑定网吧表   dbo.t_ClientInfo  网吧表  

auditStatus=0 商品表审核通过  bupd!=-1 未被删除

传入值:cdcs  =网吧编号

目的:

1.排序
a.首先查出绑定地区的商品
b.查询绑定该网吧的商品
c.剩下的其他商品

共同条件是审核通过,未被删除

问题补充: 不知道有没有描述清楚哦

问题补充:其实完整的是放在一个function里面的

如果cdcs是作为一个参数传进来的,外面可能还会用到其他过滤条件,而且还有个排序字段   order by isCommend desc/////isCommend 是dbo.tbGoodsInfo里面的一个字段,那这样的话会不会出错呢?

问题补充: 不好意思,描述里面还有点问题,
排序C,剩余其他商品(不包含绑定其他网吧的商品
)

相关问答

更多
  • 一样的条件,*是最慢的,把所有属性列出来是最好的
  • 2. 选择最有效率的表名顺序(只在基于规则的优化器中有效) ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,因此FROM子句中写在最后的表(基础表 driving table)将被最先处理. 在FROM子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表.当ORACLE处理多个表时, 会运用排序及合并的方式连接它们.首先,扫描第一个表(FROM子句中最后的那个表)并对记录进行派序,然后扫描第二个表(FROM子句中最后第二个表),最后将所有从第二个表中检索出的记录与第一个表中合适记 ...
  • 试试这个: insert into mt_seal_inst (detect_task_no, bar_code, seal_date, write_date) select 'DETECT_TASK_NO', 'strBarcode', to_char(to_date(detect_date, 'yyyy-mm-dd hh24:mi:ss') + numtodsinterval(3, 'second')), to_char(to_date(detect_date, 'yyyy-mm-dd hh24:mi: ...
  • 假设原来的句子是 select * from t1 where t1.f1 in (select t2.f2 from t2 where t2.f2=xxx) 和你的很类似 你用子查询 很慢 我们现在修改为: select t1.* from t1 ,t2 where t1.f1 = t2.f2 and t2.f2=xxxx 这里使用了关联查询代替了子查询大大提高效率。 其次你可以考虑在表t1.f1上加索引,提高查询速度。
  • select t.* from tb_User t,tb_UserCount t1 where t.Age>50 and t1.count>500 理由:1不用IN的方式查询 2.要把运算最快的条件放前面,如t.Age>50 ,且运算要放在纯数学的等式中100/2=50
  • 楼主 谁告诉你可以这样写啊 两个语句不等价 第二种写法 是隐含了asc码的比较 每个字母都对应一个asc码 这个您应该知道吧
  • sql语句优化[2022-04-27]

    select top 12 * from Product where productID not in (select top 12 productID from Product where (productName like '%2612%' or productID in (select productID from Product_Fitting where suitType like '%2612%')) and isAdjust = 0 and status = 1) and (productNa ...
  • sql语句优化[2022-08-02]

    1.查看链接查询部分是不是键和索引 2.检查d.area_id是不是有索引 3.检查a.alloc_date是不是有索引 4.将Or链接的条件改成union all的方式来查询 5.(可能是关键)to_char(a.alloc_date,'yyyymm')>='200805' 这个函数方式的,改为直接方式的a.alloc_date >= date'2008-05-01' 避免全表扫描 6.(也可能是关键)检查类型一致性,area_id in d.area_id in (17000124,17000125,1 ...
  • case when (select count(1) from latreebak v, laagentgrade u, laagentgrade w where u.gradeid >w.gradeid and u.gradecode=v.agentlastgrade and w.gradecode =v.agentgrade and v.agentcode =a.agentcode)>0 then '降级' else '未降级' end
  • 基本相同,都遵循SQL语言的语法规则。在SQL脚本上两者最大的区别是函数不一样和个别关键字不一样。比如Oracle里没有limit关键字,所以Oracle实现分页比MySQL麻烦。