关于sql性能问题,请各位大侠指教

以下是我面试遇到的几个问题,很难自己解决,请各位大侠帮忙下
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)某集团公司以及北京,南京和上海分公司使用某套企业管理系统,要求不同岗位的用户分别有不同的权限,比如主数据管理和业务操作权限等,如何实现这些不同岗位的用户之间的权限管理?请先假设需求场景,再用你最熟悉的建模语言来表达你的设计思想
这道题更是没头绪,请各位给点思想

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’

一:count(*)改为count(1),1000万条记录的业务表,count(*)与count(1)会有较为明显的性能差别;
二:明显t_plan_detail是t_plan的子表,那么用明显表去左连接主表,是毫无意义的,直接关联即可;(楼主上面说item_code是明显表的,这个SQL是不是写错了?)

(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)
上面万的记录集,用in会极其损耗性能,改为
select count(1) from t_plan a whre plan_id exists(
select 1 from t_plan_detail b where b.item_code='p004' and a.plan_id=b.plan_id) 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
一:子查询中的order by 完全没有意义;
二:子查询中的b.*严重影响性能,结果中只需要b表一个字段,而sql选出所有字段;
三:完全没有必要套一层查询,直接将子查询加上create_userid=1再修改order by就可以了。

(4)
我没有能力回答如此学术性的问题,我只能说凭经验,不会给这个字段加索引!

invsummary表可否再详细的说明一下结构及业务原理?

(5)
一:建立权限主数据,将所有业务单元维护在权限表中;
二:要求系统中存在用户、岗位主数据,且存在mapping;
三:建立制授权表,将岗位与权限mapping;
四:用户登录系统,通过用户查找岗位,再查找所有权限;
五:做一个权限检查程序,或Inc文件,或java程序,引入到每一个页面文件进行检查。
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答