日期:2014-05-18  浏览次数:20488 次

有300万数据查询速度太慢请大神们帮分析下看怎样写可以速度快些
表结构
[autoid] [int] IDENTITY(1,1) NOT NULL,
[shop_id] [int] NULL,
[mixid] [int] NULL,
[type] [int] NULL,
[typename] [nvarchar](50) NULL,
[userid] [nvarchar](50) NULL,
[url] [nvarchar](200) NULL,
[createdate] [datetime] NULL CONSTRAINT [DF_Redirect_Record_createdate] DEFAULT (getdate()),

表中有300万数据,以下查询速度太慢,咋写能快点?
以下是存储过程

CREATE PROCEDURE [dbo].[sp_Click_Shop]
@atype int,
@shop_id varchar(50)
as
--商城点击率
if(@atype=0)
select count(shop_id) as cnt from Redirect_Record where shop_id=@shop_id and DateDiff(dd,createdate,getdate())=0 
else if(@atype=1)
select count(shop_id) as cnt from Redirect_Record where shop_id=@shop_id

 


------解决方案--------------------
有个项目,一天的数据量近千万级,要不用索引要不限制客户的查询范围
------解决方案--------------------
如果可以的话,建议在shop_id上建立聚集索引。
如果这个SP很常用,最好的办法是每天进行一次数据统计,放入一张新表中,然后从这个表进行访问。
新表的机构类似如下:
shop_id int
createdate date
clickcount int