sql执行效率

webjock webjock 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

--客户表
create table TB_Customer
(
ID int identity primary key,
Name nvarchar(10),
Mobile nvarchar(10)
)
--销售单表
create table TB_Sale
(
ID int identity primary key,
Year int,--年份
CustomerID int,--客户表外键
IsCard bit default(0),--有卡片销售
IsProduct bit default(0),--有零售
SalePrice decimal(10,2)--销售额
)
--卡片表
create table TB_Card
(
ID int identity primary key,
Year int,--年份
Statu int, --1、不可以2、可用3、已用
CardNumber nvarchar(20),
CardPrice int,--卡片额度
BuyDiscount decimal(3,2),--购买折扣
CustomerID int,--客户表外键
SaleID int,--销售单表外键
)

上述三张表,其中客户表的数据10W,销售单表的数据30W,卡片表的数据500W
目前需要汇总几个数据:
1、查询所有客户的购买额度和卡片的购买额度
2、查询销售单中不同状态卡片的集合
上边2个方法,我用的是基本的查询汇总方式

--查询所有客户的购买额度和卡片的购买额度
select c.ID,C.Name,c.Mobile,
(select ISNULL(sum(s.SalePrice),0) from TB_Sale s where s.CustomerID=c.ID) as 'SalePrice',
(select ISNULL(sum(case when d.BuyDiscount>0 d.CardPrice*d.BuyDiscount then else d.CardPrice end),0) from TB_Card d where d.CustomerID=c.ID) as 'CardPrice'
from TB_Customer c

--查询销售单中不同状态卡片的集合
select * from TB_Card where Statu=1 and SaleID=@SaleID
select * from TB_Card where Statu=2 and SaleID=@SaleID
select * from TB_Card where Statu=3 and SaleID=@SaleID

我用的方法执行起来比较慢,其中我对TB_Sale中的CustomerID,TB_Card中的Statu、CustomerID、SaleID加了索引。
上述方法执行起来比较耗CPU,执行时间也特别慢,应该怎样提高查询速度和性能,需不需要做分区什么的
...全文
评论 6
收藏 已收藏
分享
返回顶部
webjock
疑难问题 22,298 成员 121,727 内容