or 和 union 测试

 

复制代码
1 /*
2 or 和 union 测试结论:当都有索引时union效率更高,
3 如果没有索引or效率高因为:union 符合 SARG条件
4  */
5
6
7  CREATE TABLE fact_sales(date_id int, product_id int, store_id int, quantity int, unit_price numeric(7,2), other_data char(1000))
8 GO
9 CREATE CLUSTERED INDEX ci ON fact_sales(date_id);
10 GO
11 PRINT 'Loading...';
12 SET NOCOUNT ON;
13
14 DECLARE @i int;
15 SET @i = 1;
16
17 WHILE (@i<100000)
18 BEGIN
19 INSERT INTO fact_sales
20 VALUES(20080800 + (@i%30) + 1, @i%10000, @i%200, RAND() * 25, (@i%3) + 1, '');
21
22 SET @i += 1;
23 END;
24 GO
25 DECLARE @i int;
26 SET @i = 1;
27
28 WHILE (@i<10000)
29 BEGIN
30 INSERT INTO fact_sales
31 VALUES(20080900 + (@i%30) + 1, @i%10000, @i%200, RAND() * 25, (@i%3) + 1, '');
32
33 SET @i += 1;
34 END;
35 PRINT 'Done.';
36 GO
37 set statistics io on
38 go
39 select distinct date_id
40 from fact_sales
41 where store_id = 23 and (unit_price = 2.00 or product_id = 23)
42 go
43 select date_id from fact_sales where store_id = 23 and (unit_price = 2.00)
44 union
45 select date_id from fact_sales where store_id = 23 and (product_id = 23)
46 go
47 select date_id from fact_sales where store_id = 23 and (unit_price = 2.00)
48 union all
49 select date_id from fact_sales where store_id = 23 and (product_id = 23)
50 go
51 set statistics io off
52
53 --create index idx_product_id on fact_sales(product_id)
54 --create index idx_store_id on fact_sales(store_id)
55 --drop index fact_sales.idx_store_id
56 --create index idx_store_id on fact_sales(unit_price)
复制代码

 

posted on   trams  阅读(814)  评论(3编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
< 2010年7月 >
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7

统计

点击右上角即可分享
微信分享提示