解決方法就是在order by ID desc再加一個(gè)排序的字段,這樣子可能會(huì)把速度提高很多。再加止排序的字段因查詢而異了
如表
復(fù)制代碼 代碼如下:
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, 1) NOT NULL ,
[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成外建關(guān)系
于是建了一個(gè)視圖
復(fù)制代碼 代碼如下:
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 '免費(fèi)' when '02' then '點(diǎn)播' 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
一開(kāi)始的查詢語(yǔ)句為
復(fù)制代碼 代碼如下:
select top 6*from [ViewCMPP_SendCentreMo]
where SendType = '扣費(fèi)'
order by id desc
發(fā)現(xiàn)非常的慢
經(jīng)過(guò)了解,原因是order by id desc/asc的查詢是一行一行的找數(shù)據(jù),所以非常的慢
于是改成了
復(fù)制代碼 代碼如下:
select top 6*from [ViewCMPP_SendCentreMo]
where SendType = '扣費(fèi)'
order by SendCentreID desc, id desc
查詢就非常的快了
您可能感興趣的文章:- SQL Server 排序函數(shù) ROW_NUMBER和RANK 用法總結(jié)
- 讓MySQL支持中文排序的實(shí)現(xiàn)方法
- sqlserver中將varchar類(lèi)型轉(zhuǎn)換為int型再進(jìn)行排序的方法
- sql多條件多字段排序(圖文教程)
- Mysql select in 按id排序?qū)崿F(xiàn)方法
- SQL字符型字段按數(shù)字型字段排序?qū)崿F(xiàn)方法
- MYSQL 關(guān)于兩個(gè)經(jīng)緯度之間的距離由近及遠(yuǎn)排序
- Mysql中的排序規(guī)則utf8_unicode_ci、utf8_general_ci的區(qū)別總結(jié)
- SQL-ORDER BY 多字段排序(升序、降序)
- SQL語(yǔ)句實(shí)現(xiàn)表中字段的組合累加排序