加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQL order by ID desc/asc加一个排序的字段解决查询慢问题

发布时间:2020-12-12 09:55:42 所属栏目:MsSql教程 来源:网络整理
导读:解决方法就是在order by ID desc再加一个排序的字段,这样子可能会把速度提高很多。再加止排序的字段因查询而异了 如表 div class="codetitle" a style="CURSOR: pointer" data="89690" class="copybut" id="copybut89690" onclick="doCopy('code89690')" 代

解决方法就是在order by ID desc再加一个排序的字段,这样子可能会把速度提高很多。再加止排序的字段因查询而异了
如表
<div class="codetitle"><a style="CURSOR: pointer" data="89690" class="copybut" id="copybut89690" onclick="doCopy('code89690')"> 代码如下:<div class="codebody" id="code89690">
CREATE TABLE [dbo].[CMPP_SendCentre] (
[id] [int] IDENTITY (1,1) NOT NULL,
[SendType] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL,
[SendDate] [datetime] NOT NULL,
[Port] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Service_ID] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL,
[FeeType] [varchar] (2) COLLATE Chinese_PRC_CI_AS NOT NULL,
[FeeCode] [varchar] (6) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Msg_Content] [varchar] (1024) COLLATE Chinese_PRC_CI_AS NOT NULL,
[SendCount] [int] NOT NULL,
[SucceedCount] [int] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[CMPP_SendCentreMo] (
[id] [int] IDENTITY (1,
[SendCentreID] [int] NOT NULL,
[Mo] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Stat] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CMPP_SendCentreMo.SendCentreID 与CMPP_SendCentre.ID成外建关系

于是建了一个视图
<div class="codetitle"><a style="CURSOR: pointer" data="81404" class="copybut" id="copybut81404" onclick="doCopy('code81404')"> 代码如下:<div class="codebody" id="code81404">
CREATE VIEW dbo.ViewCMPP_SendCentreMo
AS
SELECT
dbo.CMPP_SendCentreMo.id,
dbo.CMPP_SendCentreMo.SendCentreID,
dbo.CMPP_SendCentreMo.Mo,
dbo.CMPP_SendCentreMo.Stat,
dbo.CMPP_SendCentre.SendType,
dbo.CMPP_SendCentre.SendDate,
dbo.CMPP_SendCentre.Port,
dbo.CMPP_SendCentre.Service_ID,
case dbo.CMPP_SendCentre.FeeType when '01' then '免费' when '02' then '点播' else '包月' end as FeeType,
cast(dbo.CMPP_SendCentre.FeeCode as smallint) as FeeCode,
dbo.CMPP_SendCentre.Msg_Content
FROM dbo.CMPP_SendCentre INNER JOIN
dbo.CMPP_SendCentreMo ON
dbo.CMPP_SendCentre.id = dbo.CMPP_SendCentreMo.SendCentreID

一开始的查询语句为
<div class="codetitle"><a style="CURSOR: pointer" data="59378" class="copybut" id="copybut59378" onclick="doCopy('code59378')"> 代码如下:<div class="codebody" id="code59378">
select top 6from [ViewCMPP_SendCentreMo]
where SendType = '扣费'
order by id desc

发现非常的慢
经过了解,原因是order by id desc/asc的查询是一行一行的找数据,所以非常的慢
于是改成了
<div class="codetitle"><a style="CURSOR: pointer" data="98899" class="copybut" id="copybut98899" onclick="doCopy('code98899')"> 代码如下:<div class="codebody" id="code98899">
select top 6
from [ViewCMPP_SendCentreMo]
where SendType = '扣费'
order by SendCentreID desc,id desc

查询就非常的快了

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读