Sql 性能问题
T_plan表和t_plan_detail表为主从关系表,靠plan_id外键关联(存在索引)。
其中t_plan 100万行记录,t_plan_detail 1000万行记录,t_plan_detail.item_code上有索引。
简述下面sql语句有什么性能问题
(1)select count(*) from t_plan_detail t1 left join t_plan t2 on t1.plan_id = t2.plan_id where
t2.item_code = ‘P004’
上面提示说两处性能问题
(2)select count(1) from t_plan whre plan_id in(
Select plan_id from t_plan_detail where item_code =’p004’
) and plan status in(4,60)
(3)
Select plan_code , plan_status , create_date –create_date是t_plan_detail 的字段
From (select a.create_userid ,a.plan_code ,a..plan_status ,b*
From t_plan a ,t_plan_detail b
Where a.plan_id = b.plan_id
And b.item_code =’p004’
Order by b.create_date , a.plan_code
) X
Where x.create_userid = 1 order by x..plan_code desc
上面提示有三个性能问题
(4)
在某个数据表中有1000条记录,假设某状态字段有10个不同的值,且记录中状态的分布值会均等,那么按照该状态创建的索引的选择率是
A 10% B 50% C 90% D:100% E 都不对,请填写正确的值
库存明显帐invssummary表,存在下面一些字段
Columunname 字段含义
Year 年
Month 月
Plant 日
Productcode 产品编号
Productname 产品名称
Spec 规格
Warehouseid 仓库id
Warehousename 仓库名称
Amount 库存金额
现在抽取每个仓库每月的库存金额排名前3的产品资料 ,请给出sql语句
(5)某集团公司以及北京,南京和上海分公司使用某套企业管理系统,要求不同岗位的用户分别有不同的权限,比如主数据管理和业务操作权限等,如何实现这些不同岗位的用户之间的权限管理?请先假设需求场景,再用你最熟悉的建模语言来表达你的设计思想
这道题更是没头绪,请各位给点思想
quoteselect count(1) from t_plan whre plan_id in(
Select plan_id from t_plan_detail where item_code =’p004’
) and plan status in(4,60) [/quote]
第二个不好评论,如果Select plan_id from t_plan_detail where item_code =’p004’查询出来的结果集很小的话,这里应该没多大的性能瓶颈吧,当然,返过来说,如果查出来的结果集比较大,建议改为exists。
[code="sql"]select count(1) from t_plan whre exists(
Select plan_id from t_plan_detail where item_code =’p004’and plan_id = t_plan.plan_id
) and plan status in(4,60)[/code]
[quote](1)select count(*) from t_plan_detail t1 left join t_plan t2 on t1.plan_id = t2.plan_id where
t2.item_code = ‘P004’ [/quote]
如上文所说t_plan_detail1000万条记录,t_plan100万条记录。因此t_plan应当放到离from更近的地方。
t_plan_detail.item_code上有索引,说明t_plan和t_plan_detail表都有item_code字段。
那么就使用有索引的字段,因此SQL语句改成
[code="sql"]select count(*) from t_plan t1 right join t_plan_detail t2 on t1.plan_id = t2.plan_id where
t2.item_code = ‘P004’[/code]
quote
Select plan_code , plan_status , create_date –create_date是t_plan_detail 的字段
From (select a.create_userid ,a.plan_code ,a..plan_status ,b*
From t_plan a ,t_plan_detail b
Where a.plan_id = b.plan_id
And b.item_code =’p004’
Order by b.create_date , a.plan_code
) X
Where x.create_userid = 1 order by x..plan_code desc [/quote]
b.item_code =’p004’过滤后的结果集明显很小,因此应当放到离where语句更近的地方。
Order by b.create_date , a.plan_code,这里排序没多大意义,因为在外面又进行了一次排序。
Where x.create_userid = 1这个过滤条件,如果放在里面,那么就能过滤掉更多的数据。
[code="java"]Select plan_code , plan_status , create_date –create_date是t_plan_detail 的字段
From (select a.create_userid ,a.plan_code ,a..plan_status ,b*
From t_plan a ,t_plan_detail b
Where a.create_userid = 1
And b.item_code =’p004’ and a.plan_id = b.plan_id
) X
order by x.create_date ,x..plan_code desc [/code]
[quote]Columunname 字段含义
Year 年
Month 月
Plant 日
Productcode 产品编号
Productname 产品名称
Spec 规格
Warehouseid 仓库id
Warehousename 仓库名称
Amount 库存金额
现在抽取每个仓库每月的库存金额排名前3的产品资料[/quote]
如果是oracle,这个简单,使用分析函数。
[url]http://huajiang.iteye.com/blog/483253[/url]
随便写下,不一定通过,自己调试
[code="sql"]select invssummary.*,rank() over(partition by Warehousedid,Year,Mounth order by Amount desc) _rk where _rk>3[/code]
[quote](5)某集团公司以及北京,南京和上海分公司使用某套企业管理系统,要求不同岗位的用户分别有不同的权限,比如主数据管理和业务操作权限等,如何实现这些不同岗位的用户之间的权限管理?请先假设需求场景,再用你最熟悉的建模语言来表达你的设计思想 [/quote]
太抽象了,不解了,谁补上