- 浏览: 91889 次
- 性别:
- 来自: 上海
文章分类
最新评论
A:索引的使用
--数据库的执行效率很大程度上取决于索引的建立,具体建索引原则,很多文章都有介绍,略.
--主要介绍一些用SQL语句建索引的方法,和维护索引的方法:
/*
建索引
*/
--建普通索引
create index 索引名称 on 表名(字段)
--建聚集索引
create clustered index 索引名称 on 表名(字段)
--建非聚集索引
create NONCLUSTERED index 索引名称 on 表名(字段)
/*
删除索引
*/
drop index 表名.索引名称
/*
使用索引
*/
情况1:
如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。如下面语句将进行全表扫描:
select id from t where num=@num
可以改为强制查询使用索引:
select id from t with(index(索引名)) where num=@num
情况2:
使用索引查询:
select * from 表名(index =索引名) where 索引字段=50100
B:连接查询没有子查询效率高
大量的左查询、连接效率没有子查询效率高!
--案例一:
存储过程如下:
CREATE PROCEDURE P_TEST @Date INT
AS
SELECT
F2_A001,
F15_A001,
SplitStyle = F11_A074,
[Schema] = Isnull('规则一' + Convert(varchar(100), SongGu) + '单位一; ', '')
+ Isnull('规则二+ Convert(varchar(100), XianJin) + '单位二; ', '')
+ Isnull('规则三 + Convert(varchar(100), SuoGu / 10.0) + '单位三; ', '')
+ Isnull('规则四+ Convert(varchar(100), RenGou) + '单位四; ', '')
+ Isnull('规则六+ Convert(varchar(100), RenGu) + '单位五; ', '')
+ Isnull('规则七' + Convert(varchar(100), ZhiGongGu) + '单位六; ', '')
+ Isnull('规则八' + Convert(varchar(100), GFenHong) + '单位七; ', '')
+ Isnull('规则九' + Convert(varchar(100), GSongGu) + '单位八; ', '')
FROM (
SELECT DISTINCT A.F2_A001, A.F15_A001, F11_A074 = (SELECT TOP 1 F11_A074 FROM A_074 C WHERE F1_A074 = B.F1_A074 AND F18_A074 / 100 = B.F18_A074 / 100 ORDER BY F18_A074 DESC),
SongGu = ISNULL(ISNULL(A1.F14_A075,A3.F14_A075),A2.F14_A075),
XianJin = ISNULL(ISNULL(B1.F14_A075,B3.F14_A075),B2.F14_A075),
SuoGu = ISNULL(ISNULL(I1.F25_A075,I3.F25_A075),I2.F25_A075),
RenGou = ISNULL(ISNULL(C1.F25_A075,C3.F25_A075),C2.F25_A075),
RenGu = ISNULL(ISNULL(D1.F25_A075,D3.F25_A075),D2.F25_A075),
ZhiGongGu = ISNULL(ISNULL(J1.F14_A075,J3.F25_A075),J2.F25_A075),
GFenHong = ISNULL(ISNULL(F1.F10_A075,F3.F25_A075),F2.F25_A075),
GSongGu = ISNULL(ISNULL(G1.F14_A075,G3.F25_A075),G2.F25_A075)
from a_001 a INNER JOIN a_074 B
ON A.OID_A001 = B.F1_A074 AND F18_A074/100= @Date
left join A_075 A1 on A1.F1_A075=a.OID_A001 AND A1.F3_A075 = '4A1'
left join A_075 A3 on A3.F1_A075=a.OID_A001 AND A3.F3_A075 = '4A3'
left join A_075 A2 on A2.F1_A075=a.OID_A001 AND A2.F3_A075 = '4A2'
left join A_075 B1 on B1.F1_A075=a.OID_A001 AND B1.F3_A075 = '4B1'
left join A_075 B3 on B3.F1_A075=a.OID_A001 AND B3.F3_A075 = '4B3'
left join A_075 B2 on B2.F1_A075=a.OID_A001 AND B2.F3_A075 = '4B2'
left join A_075 I1 on I1.F1_A075=a.OID_A001 AND I1.F3_A075 = '4I1'
left join A_075 I3 on I3.F1_A075=a.OID_A001 AND I3.F3_A075 = '4I3'
left join A_075 I2 on I2.F1_A075=a.OID_A001 AND I2.F3_A075 = '4I2'
left join A_075 C1 on C1.F1_A075=a.OID_A001 AND C1.F3_A075 = '4C1'
left join A_075 C3 on C3.F1_A075=a.OID_A001 AND C3.F3_A075 = '4C3'
left join A_075 C2 on C2.F1_A075=a.OID_A001 AND C2.F3_A075 = '4C2'
left join A_075 D1 on D1.F1_A075=a.OID_A001 AND D1.F3_A075 = '4D1'
left join A_075 D3 on D3.F1_A075=a.OID_A001 AND D3.F3_A075 = '4D3'
left join A_075 D2 on D2.F1_A075=a.OID_A001 AND D2.F3_A075 = '4D2'
left join A_075 J1 on J1.F1_A075=a.OID_A001 AND J1.F3_A075 = '4J1'
left join A_075 J3 on J3.F1_A075=a.OID_A001 AND J3.F3_A075 = '4J3'
left join A_075 J2 on J2.F1_A075=a.OID_A001 AND J2.F3_A075 = '4J2'
left join A_075 F1 on F1.F1_A075=a.OID_A001 AND F1.F3_A075 = '4F1'
left join A_075 F3 on F3.F1_A075=a.OID_A001 AND F3.F3_A075 = '4F3'
left join A_075 F2 on F2.F1_A075=a.OID_A001 AND F2.F3_A075 = '4F2'
left join A_075 G1 on G1.F1_A075=a.OID_A001 AND G1.F3_A075 = '4G1'
left join A_075 G3 on G3.F1_A075=a.OID_A001 AND G3.F3_A075 = '4G3'
left join A_075 G2 on G2.F1_A075=a.OID_A001 AND G2.F3_A075 = '4G2'
) T
--数据量情况
select * from a_074
记录数:730
select * from a_001
记录数:2424
select * from a_075
记录数:30028
--执行后,需要耗费时间为: 1分钟48秒,经常查询超时
分析消耗资源的点:
--1:从存储过程中可以看到,查询用到了很多左连接,左连接消耗了大量资源
--2:查询条件中用到变量,要导致全表扫描,消耗大量资源
--解决办法:
--1:把所有左连接更改为子查询
--2:把查询条件,强制指定索引
步骤:
A:改为子查询如下:
CREATE PROCEDURE P_TEST @Date INT
AS
SELECT
F2_A001,
F15_A001,
SplitStyle = F11_A074,
[Schema] = Isnull('规则一' + Convert(varchar(100), SongGu) + '单位一; ', '')
+ Isnull('规则二+ Convert(varchar(100), XianJin) + '单位二; ', '')
+ Isnull('规则三 + Convert(varchar(100), SuoGu / 10.0) + '单位三; ', '')
+ Isnull('规则四+ Convert(varchar(100), RenGou) + '单位四; ', '')
+ Isnull('规则六+ Convert(varchar(100), RenGu) + '单位五; ', '')
+ Isnull('规则七' + Convert(varchar(100), ZhiGongGu) + '单位六; ', '')
+ Isnull('规则八' + Convert(varchar(100), GFenHong) + '单位七; ', '')
+ Isnull('规则九' + Convert(varchar(100), GSongGu) + '单位八; ', '')
FROM (
SELECT DISTINCT A.F2_A001, A.F15_A001, F11_A074 = (SELECT TOP 1 F11_A074 FROM A_074 C WHERE F1_A074 = B.F1_A074 AND F18_A074 / 100 = B.F18_A074 / 100 ORDER BY F18_A074 DESC),
SongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A2')),
XianJin = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B2')),
SuoGu = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I2')),
RenGou = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C2')),
RenGu = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D2')),
ZhiGongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J2')),
GFenHong = ISNULL(ISNULL((SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F1'), (SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F3')), (SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F2')),
GSongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G2'))
from a_001 a INNER JOIN a_074 B
ON A.OID_A001 = B.F1_A074 AND F18_A074/100= @SEQ
) T
B:建索引
1:为表a_074 建聚集索引
create clustered index Index_a_074_F18_A074 on a_074(F18_A074)
2:为表A_075建非聚集索引
create NONCLUSTERED index Index_A_075_F3_A075 on A_075(F3_A075)
强制在查询条件使用索引后的存储过程如下:
CREATE PROCEDURE P_TEST @Date INT
AS
SELECT
F2_A001,
F15_A001,
SplitStyle = F11_A074,
[Schema] = Isnull('规则一' + Convert(varchar(100), SongGu) + '单位一; ', '')
+ Isnull('规则二+ Convert(varchar(100), XianJin) + '单位二; ', '')
+ Isnull('规则三 + Convert(varchar(100), SuoGu / 10.0) + '单位三; ', '')
+ Isnull('规则四+ Convert(varchar(100), RenGou) + '单位四; ', '')
+ Isnull('规则六+ Convert(varchar(100), RenGu) + '单位五; ', '')
+ Isnull('规则七' + Convert(varchar(100), ZhiGongGu) + '单位六; ', '')
+ Isnull('规则八' + Convert(varchar(100), GFenHong) + '单位七; ', '')
+ Isnull('规则九' + Convert(varchar(100), GSongGu) + '单位八; ', '')
FROM (
SELECT DISTINCT A.F2_A001, A.F15_A001, F11_A074 = (SELECT TOP 1 F11_A074 FROM A_074 C WHERE F1_A074 = B.F1_A074 AND F18_A074 / 100 = B.F18_A074 / 100 ORDER BY F18_A074 DESC),
SongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A2')),
XianJin = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B2')),
SuoGu = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I2')),
RenGou = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C2')),
RenGu = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D2')),
ZhiGongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J2')),
GFenHong = ISNULL(ISNULL((SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F1'), (SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F3')), (SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F2')),
GSongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G2'))
from a_001 a INNER JOIN a_074 B with(index(Index_a_074_F18_A074)) --强制使用索引
ON A.OID_A001 = B.F1_A074 AND F18_A074/100= @SEQ
) T
--数据库的执行效率很大程度上取决于索引的建立,具体建索引原则,很多文章都有介绍,略.
--主要介绍一些用SQL语句建索引的方法,和维护索引的方法:
/*
建索引
*/
--建普通索引
create index 索引名称 on 表名(字段)
--建聚集索引
create clustered index 索引名称 on 表名(字段)
--建非聚集索引
create NONCLUSTERED index 索引名称 on 表名(字段)
/*
删除索引
*/
drop index 表名.索引名称
/*
使用索引
*/
情况1:
如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。如下面语句将进行全表扫描:
select id from t where num=@num
可以改为强制查询使用索引:
select id from t with(index(索引名)) where num=@num
情况2:
使用索引查询:
select * from 表名(index =索引名) where 索引字段=50100
B:连接查询没有子查询效率高
大量的左查询、连接效率没有子查询效率高!
--案例一:
存储过程如下:
CREATE PROCEDURE P_TEST @Date INT
AS
SELECT
F2_A001,
F15_A001,
SplitStyle = F11_A074,
[Schema] = Isnull('规则一' + Convert(varchar(100), SongGu) + '单位一; ', '')
+ Isnull('规则二+ Convert(varchar(100), XianJin) + '单位二; ', '')
+ Isnull('规则三 + Convert(varchar(100), SuoGu / 10.0) + '单位三; ', '')
+ Isnull('规则四+ Convert(varchar(100), RenGou) + '单位四; ', '')
+ Isnull('规则六+ Convert(varchar(100), RenGu) + '单位五; ', '')
+ Isnull('规则七' + Convert(varchar(100), ZhiGongGu) + '单位六; ', '')
+ Isnull('规则八' + Convert(varchar(100), GFenHong) + '单位七; ', '')
+ Isnull('规则九' + Convert(varchar(100), GSongGu) + '单位八; ', '')
FROM (
SELECT DISTINCT A.F2_A001, A.F15_A001, F11_A074 = (SELECT TOP 1 F11_A074 FROM A_074 C WHERE F1_A074 = B.F1_A074 AND F18_A074 / 100 = B.F18_A074 / 100 ORDER BY F18_A074 DESC),
SongGu = ISNULL(ISNULL(A1.F14_A075,A3.F14_A075),A2.F14_A075),
XianJin = ISNULL(ISNULL(B1.F14_A075,B3.F14_A075),B2.F14_A075),
SuoGu = ISNULL(ISNULL(I1.F25_A075,I3.F25_A075),I2.F25_A075),
RenGou = ISNULL(ISNULL(C1.F25_A075,C3.F25_A075),C2.F25_A075),
RenGu = ISNULL(ISNULL(D1.F25_A075,D3.F25_A075),D2.F25_A075),
ZhiGongGu = ISNULL(ISNULL(J1.F14_A075,J3.F25_A075),J2.F25_A075),
GFenHong = ISNULL(ISNULL(F1.F10_A075,F3.F25_A075),F2.F25_A075),
GSongGu = ISNULL(ISNULL(G1.F14_A075,G3.F25_A075),G2.F25_A075)
from a_001 a INNER JOIN a_074 B
ON A.OID_A001 = B.F1_A074 AND F18_A074/100= @Date
left join A_075 A1 on A1.F1_A075=a.OID_A001 AND A1.F3_A075 = '4A1'
left join A_075 A3 on A3.F1_A075=a.OID_A001 AND A3.F3_A075 = '4A3'
left join A_075 A2 on A2.F1_A075=a.OID_A001 AND A2.F3_A075 = '4A2'
left join A_075 B1 on B1.F1_A075=a.OID_A001 AND B1.F3_A075 = '4B1'
left join A_075 B3 on B3.F1_A075=a.OID_A001 AND B3.F3_A075 = '4B3'
left join A_075 B2 on B2.F1_A075=a.OID_A001 AND B2.F3_A075 = '4B2'
left join A_075 I1 on I1.F1_A075=a.OID_A001 AND I1.F3_A075 = '4I1'
left join A_075 I3 on I3.F1_A075=a.OID_A001 AND I3.F3_A075 = '4I3'
left join A_075 I2 on I2.F1_A075=a.OID_A001 AND I2.F3_A075 = '4I2'
left join A_075 C1 on C1.F1_A075=a.OID_A001 AND C1.F3_A075 = '4C1'
left join A_075 C3 on C3.F1_A075=a.OID_A001 AND C3.F3_A075 = '4C3'
left join A_075 C2 on C2.F1_A075=a.OID_A001 AND C2.F3_A075 = '4C2'
left join A_075 D1 on D1.F1_A075=a.OID_A001 AND D1.F3_A075 = '4D1'
left join A_075 D3 on D3.F1_A075=a.OID_A001 AND D3.F3_A075 = '4D3'
left join A_075 D2 on D2.F1_A075=a.OID_A001 AND D2.F3_A075 = '4D2'
left join A_075 J1 on J1.F1_A075=a.OID_A001 AND J1.F3_A075 = '4J1'
left join A_075 J3 on J3.F1_A075=a.OID_A001 AND J3.F3_A075 = '4J3'
left join A_075 J2 on J2.F1_A075=a.OID_A001 AND J2.F3_A075 = '4J2'
left join A_075 F1 on F1.F1_A075=a.OID_A001 AND F1.F3_A075 = '4F1'
left join A_075 F3 on F3.F1_A075=a.OID_A001 AND F3.F3_A075 = '4F3'
left join A_075 F2 on F2.F1_A075=a.OID_A001 AND F2.F3_A075 = '4F2'
left join A_075 G1 on G1.F1_A075=a.OID_A001 AND G1.F3_A075 = '4G1'
left join A_075 G3 on G3.F1_A075=a.OID_A001 AND G3.F3_A075 = '4G3'
left join A_075 G2 on G2.F1_A075=a.OID_A001 AND G2.F3_A075 = '4G2'
) T
--数据量情况
select * from a_074
记录数:730
select * from a_001
记录数:2424
select * from a_075
记录数:30028
--执行后,需要耗费时间为: 1分钟48秒,经常查询超时
分析消耗资源的点:
--1:从存储过程中可以看到,查询用到了很多左连接,左连接消耗了大量资源
--2:查询条件中用到变量,要导致全表扫描,消耗大量资源
--解决办法:
--1:把所有左连接更改为子查询
--2:把查询条件,强制指定索引
步骤:
A:改为子查询如下:
CREATE PROCEDURE P_TEST @Date INT
AS
SELECT
F2_A001,
F15_A001,
SplitStyle = F11_A074,
[Schema] = Isnull('规则一' + Convert(varchar(100), SongGu) + '单位一; ', '')
+ Isnull('规则二+ Convert(varchar(100), XianJin) + '单位二; ', '')
+ Isnull('规则三 + Convert(varchar(100), SuoGu / 10.0) + '单位三; ', '')
+ Isnull('规则四+ Convert(varchar(100), RenGou) + '单位四; ', '')
+ Isnull('规则六+ Convert(varchar(100), RenGu) + '单位五; ', '')
+ Isnull('规则七' + Convert(varchar(100), ZhiGongGu) + '单位六; ', '')
+ Isnull('规则八' + Convert(varchar(100), GFenHong) + '单位七; ', '')
+ Isnull('规则九' + Convert(varchar(100), GSongGu) + '单位八; ', '')
FROM (
SELECT DISTINCT A.F2_A001, A.F15_A001, F11_A074 = (SELECT TOP 1 F11_A074 FROM A_074 C WHERE F1_A074 = B.F1_A074 AND F18_A074 / 100 = B.F18_A074 / 100 ORDER BY F18_A074 DESC),
SongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A2')),
XianJin = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B2')),
SuoGu = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I2')),
RenGou = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C2')),
RenGu = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D2')),
ZhiGongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J2')),
GFenHong = ISNULL(ISNULL((SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F1'), (SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F3')), (SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F2')),
GSongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G2'))
from a_001 a INNER JOIN a_074 B
ON A.OID_A001 = B.F1_A074 AND F18_A074/100= @SEQ
) T
B:建索引
1:为表a_074 建聚集索引
create clustered index Index_a_074_F18_A074 on a_074(F18_A074)
2:为表A_075建非聚集索引
create NONCLUSTERED index Index_A_075_F3_A075 on A_075(F3_A075)
强制在查询条件使用索引后的存储过程如下:
CREATE PROCEDURE P_TEST @Date INT
AS
SELECT
F2_A001,
F15_A001,
SplitStyle = F11_A074,
[Schema] = Isnull('规则一' + Convert(varchar(100), SongGu) + '单位一; ', '')
+ Isnull('规则二+ Convert(varchar(100), XianJin) + '单位二; ', '')
+ Isnull('规则三 + Convert(varchar(100), SuoGu / 10.0) + '单位三; ', '')
+ Isnull('规则四+ Convert(varchar(100), RenGou) + '单位四; ', '')
+ Isnull('规则六+ Convert(varchar(100), RenGu) + '单位五; ', '')
+ Isnull('规则七' + Convert(varchar(100), ZhiGongGu) + '单位六; ', '')
+ Isnull('规则八' + Convert(varchar(100), GFenHong) + '单位七; ', '')
+ Isnull('规则九' + Convert(varchar(100), GSongGu) + '单位八; ', '')
FROM (
SELECT DISTINCT A.F2_A001, A.F15_A001, F11_A074 = (SELECT TOP 1 F11_A074 FROM A_074 C WHERE F1_A074 = B.F1_A074 AND F18_A074 / 100 = B.F18_A074 / 100 ORDER BY F18_A074 DESC),
SongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4A2')),
XianJin = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4B2')),
SuoGu = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4I2')),
RenGou = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4C2')),
RenGu = ISNULL(ISNULL((SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D1'), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D3')), (SELECT NULLIF(F25_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4D2')),
ZhiGongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4J2')),
GFenHong = ISNULL(ISNULL((SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F1'), (SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F3')), (SELECT NULLIF(F10_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4F2')),
GSongGu = ISNULL(ISNULL((SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G1'), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G3')), (SELECT NULLIF(F14_A075, 0.0) FROM A_075 WHERE F1_A075=a.OID_A001 AND F3_A075 = '4G2'))
from a_001 a INNER JOIN a_074 B with(index(Index_a_074_F18_A074)) --强制使用索引
ON A.OID_A001 = B.F1_A074 AND F18_A074/100= @SEQ
) T
发表评论
-
转载 NoSQL非关系型数据库学习 这样对比下HBase, Memcached, MongoDB, Redis和Solr
2014-11-14 16:27 0Name HBase Memc ... -
Oracle创建dblink报错:ORA-01017、ORA-02063解决 (转载)
2013-07-23 17:09 1551Oracle创建dblink报错:ORA-01017、ORA ... -
ORACLE查看并修改最大连接数
2012-11-15 17:09 0阅读:1157次 时间:2010-03-15 2 ... -
oracle apache服务占用80端口
2012-06-19 09:27 763Posted on 2008-04-28 13:17 alle ... -
DataStage 简介 (
2012-05-18 14:01 1113DataStage 简介 (2010-05-27 19 ... -
oracle常用命令
2012-05-09 13:38 01.增加主键 alter table TABLE_NAM ... -
esri 常见几何对象的构造
2012-05-03 16:24 872常见几何对象的构造 介绍ESRI的ST_GEOMETRY的几 ... -
GIS 中地理坐标和屏幕坐标的标准转换方法
2012-05-03 16:10 1427GIS 中地理坐标和屏幕坐标的标准转换方法 在GIS中,当你 ... -
Oracle Spatial的简单应用补充——shapefile导入ORACLE
2012-05-03 15:32 1029Oracle Spatial的简单应用补充——shapefil ... -
weblgic 中oracle rac的配置
2012-05-02 18:55 0通过 select * from gv$instance;来查 ... -
Oracle 10g RAC的负载均衡配置
2012-04-27 12:32 0Oracle 10g RAC的负载均衡配置 负载均衡是指连接 ... -
weblogic集群配置
2012-04-26 11:00 0weblogic集群配置 -
数据库名,数据库实例名SID,数据库服务名,全局数据库名
2012-04-26 10:20 913数据库名,数据库实例名SID,数据库服务名,全局数据库名 一、 ... -
weblogic启动与关闭的自动登陆总结
2012-04-25 19:58 995weblogic启动与关闭的自 ... -
weblogic 10 linux
2012-04-23 23:59 1052[日期:2010-06-13] 来源:Linux社区 作者: ... -
OJDBC版本区别 [ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别]
2012-03-07 11:01 1086OJDBC版本区别 [ojdbc14.jar,ojdbc5.j ... -
oracle Spatial 函数
2011-12-22 15:25 2214Oracle_spatial的函数 一sdo_Geom包的函数 ... -
oracel表类型
2011-10-26 15:19 0表类型: 1堆组织表(heap organized table ... -
oracle table函数
2011-08-24 16:56 2040PL/SQL表---table()函数用法 /* PL/SQ ... -
收藏 执行计划
2011-08-19 15:32 666如何看懂ORACLE执行计划 一、什么是执行计划 An exp ...
相关推荐
### Oracle强制索引详解 在Oracle数据库管理过程中,优化查询性能是提高系统效率的关键环节之一。其中,**强制索引**是一种重要的优化手段,它允许数据库管理员或开发人员指定特定的执行计划,确保查询能够利用预设...
在Oracle数据库中,强制索引是一种技术,允许数据库管理员或开发人员在特定查询中指定必须使用的索引,即使Oracle优化器认为全表扫描更有效。强制索引有时可以帮助提升查询性能,尤其是在对业务理解深入且确信索引能...
当你希望数据库强制使用某个特定的索引而不是默认选择时,可以使用`FORCE INDEX`。例如: ```sql SELECT * FROM TABLE1 FORCE INDEX (FIELD1) … ``` 这条SQL语句将只使用FIELD1上的索引,而忽略其他可能存在的...
强制索引查询条件 前面我们也讲了一点强制索引查询的知识,本节我们再来完整的讲述下 (1)SQL Server使用默认索引 USE TSQL2012 GO SELECT * FROM Sales.Orders 上述就不用我再啰嗦了,使用默认主键创建的聚集...
#### 显示索引(强制索引):提升查询效率的关键策略 在数据库管理与优化领域,索引的合理运用被视为提高SQL查询性能的核心技术之一。特别是对于大型数据集,正确地利用索引能够显著减少查询时间,从而提升整体系统...
唯一索引用于确保索引字段不存储重复的值,即强制索引字段的唯一性。缺省情况下,MongoDB的_id字段在创建集合的时候会自动创建一个唯一索引。本文主要描述唯一索引的用法。 关于什么是索引以及唯一索引这里就不做...
* 在作为主键的列上创建索引,强制该列的唯一性和组织表中数据的排列结构。 * 在经常用于连接的列上创建索引,加快连接的速度。 * 在经常需要根据范围进行搜索的列上创建索引,因为索引已经排序,其指定的范围是连续...
2. 唯一索引:与主键索引类似,它也确保索引字段的值唯一,但并不强制作为主键。 3. 普通索引(非唯一索引):允许索引字段的值重复,是最基础的索引类型。 4. 全文索引:适用于文本字段,用于搜索文本中的关键词...
### 索引介绍:聚集索引与非聚集索引 #### 一、索引的基本概念 在数据库中,索引是一种特殊的文件结构,它的主要目的是为了提高数据检索的速度。索引通过创建一种数据结构(例如B树)来实现这一点,这种结构允许...
3. 强制索引更新:在仓库的设置或管理页面,寻找“Reindex”、“Update Index”或类似选项,点击执行这个操作。这个动作会触发Nexus立即开始处理仓库中所有组件的索引更新。 4. 监控更新进度:更新过程中,Nexus...
《创建索引和调优索引:SQL Server 2005中的索引管理》 在SQL Server 2005中,索引是数据库管理的关键要素,它们极大地提升了数据检索速度,尤其是在处理大规模数据时。索引可以分为多种类型,包括聚集索引和非聚集...
### 分区索引—本地索引与全局索引的区别 #### 一、Oracle分区索引概念及分类 在Oracle数据库中,分区索引是针对分区表的一种特殊索引类型,它可以显著提高对于大规模数据集的查询性能。根据索引是否与表的分区...
Oracle Index索引无效的原因可能涉及多种因素,这些因素可能导致索引无法被数据库有效利用,从而影响SQL查询的性能。在解决此类问题时,首先要确定索引是否被启用和使用。以下是一些常见的索引无效或未被利用的原因...
MySQL索引 聚集索引 如果你想了解MySQL索引查询优化,你首先应该对MySQL数据组织结构、B-Tree索引、聚集索引,次要索引有一定的了解,才能够更好地理解MySQL查询优化行为。这里主要探讨MySQL InnoDB的聚集索引。
Oracle数据库中的索引是优化查询性能的关键工具,它...因此,当遇到常见的索引问题时,如索引未被使用、索引碎片化、索引过多或过少等,都需要深入分析并采取相应的解决方案,如重建索引、优化查询语句或调整索引策略。
唯一索引确保索引列中的每个值都是唯一的,主键索引是一种特殊的唯一索引,它强制表的主键列中的值必须是唯一的,并且通常用于快速访问数据。聚集索引则决定了数据的物理存储顺序。 创建索引时,应该考虑多个因素,...
数据库索引