sql这样写会不会很慢。。

极客诗人 2018-06-12 10:38:40
...全文
248 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
flybirding10011 2018-06-13
  • 打赏
  • 举报
回复
过来学习一下
极客诗人 2018-06-12
  • 打赏
  • 举报
回复
引用 9 楼 yenange 的回复:
USE tempdb
GO
IF OBJECT_ID('table_a') IS NOT NULL DROP TABLE table_a
IF OBJECT_ID('table_b') IS NOT NULL DROP TABLE table_b
IF OBJECT_ID('table_c') IS NOT NULL DROP TABLE table_c
IF OBJECT_ID('table_d') IS NOT NULL DROP TABLE table_d
GO
--订单
create table table_a
(
	Id int primary key identity(1,1),
	TypeId int ,
	ServerId int,
	UserId int,
)
--用户
create table table_b
(
	Id int primary key identity (1,1),
	UserName nvarchar(50)
)
--服务类1
create table table_c
(
	Id int primary key identity(1,1),
	ServerName nvarchar(50)
)
--服务类2
create table table_d
(
	Id int primary key identity(1,1),
	ServiceName nvarchar(50)
)
----------------- 预备 ---------------------
--0. 增加连接字段的索引
--a
CREATE INDEX ix_table_a_UserId ON table_a(UserId);
CREATE INDEX ix_table_a_ServerId ON table_a(ServerId,TypeId);
IF OBJECT_ID('tempdb..#tmp_B') IS NOT NULL
	DROP TABLE #tmp_B
----------------- 查询 ---------------------
--1. 
SELECT t2.*
INTO #tmp_B
FROM table_b t2 WITH(NOLOCK)
where t2.UserName like '%2'
--2.
CREATE UNIQUE CLUSTERED INDEX ix_#tmp_B_id ON #tmp_B(Id);
--3.
select t1.Id as ID,t2.UserName as UserName,
case
t1.TypeId 
when 1 THEN t3.ServerName
when 2 THEN t4.ServiceName
ELSE ''
END as ServerName
from table_a t1  WITH(NOLOCK)
left join #tmp_B t2  WITH(NOLOCK) on t1.UserId=t2.Id
left join table_c t3 WITH(NOLOCK) on  (t1.TypeId=1 and t1.ServerId=t3.Id)
left join table_d t4 WITH(NOLOCK) on (t1.ServerId=t4.Id and t1.TypeId=2)
--4. 删除临时表
DROP TABLE #tmp_B
过程麻烦了一点, 但思路很简单, 就是分了几步而已。 服务器稍好一点, 应付几百万的数据没什么问题, 1秒出数据。
好的 谢谢了 我去研究研究
吉普赛的歌 2018-06-12
  • 打赏
  • 举报
回复
USE tempdb
GO
IF OBJECT_ID('table_a') IS NOT NULL DROP TABLE table_a
IF OBJECT_ID('table_b') IS NOT NULL DROP TABLE table_b
IF OBJECT_ID('table_c') IS NOT NULL DROP TABLE table_c
IF OBJECT_ID('table_d') IS NOT NULL DROP TABLE table_d
GO
--订单
create table table_a
(
	Id int primary key identity(1,1),
	TypeId int ,
	ServerId int,
	UserId int,
)
--用户
create table table_b
(
	Id int primary key identity (1,1),
	UserName nvarchar(50)
)
--服务类1
create table table_c
(
	Id int primary key identity(1,1),
	ServerName nvarchar(50)
)
--服务类2
create table table_d
(
	Id int primary key identity(1,1),
	ServiceName nvarchar(50)
)
----------------- 预备 ---------------------
--0. 增加连接字段的索引
--a
CREATE INDEX ix_table_a_UserId ON table_a(UserId);
CREATE INDEX ix_table_a_ServerId ON table_a(ServerId,TypeId);
IF OBJECT_ID('tempdb..#tmp_B') IS NOT NULL
	DROP TABLE #tmp_B
----------------- 查询 ---------------------
--1. 
SELECT t2.*
INTO #tmp_B
FROM table_b t2 WITH(NOLOCK)
where t2.UserName like '%2'
--2.
CREATE UNIQUE CLUSTERED INDEX ix_#tmp_B_id ON #tmp_B(Id);
--3.
select t1.Id as ID,t2.UserName as UserName,
case
t1.TypeId 
when 1 THEN t3.ServerName
when 2 THEN t4.ServiceName
ELSE ''
END as ServerName
from table_a t1  WITH(NOLOCK)
left join #tmp_B t2  WITH(NOLOCK) on t1.UserId=t2.Id
left join table_c t3 WITH(NOLOCK) on  (t1.TypeId=1 and t1.ServerId=t3.Id)
left join table_d t4 WITH(NOLOCK) on (t1.ServerId=t4.Id and t1.TypeId=2)
--4. 删除临时表
DROP TABLE #tmp_B
过程麻烦了一点, 但思路很简单, 就是分了几步而已。 服务器稍好一点, 应付几百万的数据没什么问题, 1秒出数据。
极客诗人 2018-06-12
  • 打赏
  • 举报
回复

--订单
create table table_a
(
	Id int primary key identity(1,1),
	TypeId int ,
	ServerId int,
	UserId int,
)
--用户
create table table_b
(
	Id int primary key identity (1,1),
	UserName nvarchar(50)
)
--服务类1
create table table_c
(
	Id int primary key identity(1,1),
	ServerName nvarchar(50)
)
--服务类2
create table table_d
(
	Id int primary key identity(1,1),
	ServiceName nvarchar(50)
)
select t1.Id as ID,t2.UserName as UserName,
case
t1.TypeId 
when 1 THEN t3.ServerName
when 2 THEN t4.ServiceName
ELSE ''
END as ServerName
from table_a t1
left join table_b t2 on t1.UserId=t2.Id
left join table_c t3 on  (t1.TypeId=1 and t1.ServerId=t3.Id)
left join table_d t4 on (t1.ServerId=t4.Id and t1.TypeId=2)
where UserName like '%2'
极客诗人 2018-06-12
  • 打赏
  • 举报
回复
引用 3 楼 roy_88 的回复:
可以这样建,在条件列上建上索引,UserName这样的条件是用不到索引通常会造成表扫描,语句用到索引时不会慢
可是这里是需要进行一个用户名的模糊查询
极客诗人 2018-06-12
  • 打赏
  • 举报
回复
引用 2 楼 yenange 的回复:
慢不慢你心里有数吧? 另外, UserName 是哪个表的字段?
是用户表的 用户姓名
吉普赛的歌 2018-06-12
  • 打赏
  • 举报
回复
哦,是用户表的。 你把所有的脚本的文本贴出来吧, 一个个地敲很浪费时间
OwenZeng_DBA 2018-06-12
  • 打赏
  • 举报
回复
这样的模糊查询用不了索引,如果表大小超过100 万,会挺慢的。
中国风 2018-06-12
  • 打赏
  • 举报
回复
可以这样建,在条件列上建上索引,UserName这样的条件是用不到索引通常会造成表扫描,语句用到索引时不会慢
吉普赛的歌 2018-06-12
  • 打赏
  • 举报
回复
慢不慢你心里有数吧? 另外, UserName 是哪个表的字段?
极客诗人 2018-06-12
  • 打赏
  • 举报
回复
在线等

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧