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

帮忙SqL 语句优化及建立索引问题
NewsList表
[sql]CREATE TABLE [dbo].[NewsList] (
[NewsID] [bigint] NOT NULL ,
[UserID] [bigint] NULL ,
[ChannelID] [int] NULL ,
[ArticleTitle] [text] COLLATE Chinese_PRC_CI_AS NULL ,
[ArticleUrl] [varchar] (300) COLLATE Chinese_PRC_CI_AS NULL ,
[NewsTypeID] [int] NULL ,
[NewsDate] [datetime] NULL ,
[ZjetDate] [datetime] NULL ,
[ArticleModleID] [int] NULL ,
[CreateDate] [datetime] NULL 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO[/sql]
NewsType
[sql]
CREATE TABLE [dbo].[NewsType] (
[NewsTypeID] [int] NULL ,
[TypeName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL 
) ON [PRIMARY]
GO[/sql]
==============
目前用的是简单的查询语句(列出Articletitle,ArticleUrl,TypeName,NewsDate)
Select Articletitle,ArticleUrl,TypeName,NewsDate From NewsList,NewsType where NewsList.NewsTypeid=NewsType.Newstypeid
==============(列出Articletitle,ArticleUrl,TypeName,NewsDate及根据newstypeid相关的typename)
Select Articletitle,ArticleUrl,TypeName,NewsDate From NewsList,NewsType where NewsList.NewsTypeid=NewsType.Newstypeid and newslist.newstypeid=2
=================
数据量比较大,好几百万,有没有速度比较快的查询语句去查询;或是好的解决方案,如何建索引、视图、游标去实现根据articletitle查询?

------解决方案--------------------
SQL code
--索引资料
drop table index_tb
create table index_tb(id int constraint ck_id default 0,name varchar(20),xb varchar(2))
--查看表
exec   sp_helpconstraint  index_tb 
--键不唯一,非聚集索引
create index id_index on index_tb(id)
--删除索引
drop index index_tb.id_index
--键聚集索引
create clustered index id_index on index_tb(id) 
--删除索引
drop index index_tb.id_index
--创建check约束
alter table index_tb add constraint ck_index CHECK (name like '[0-9][0-9][0-9][0-9][0-9]')
alter table index_tb add constraint ck2_index check(xb in ('MM','GG'))
--删除check约束
alter table index_tb drop constraint ck2_index
--禁止表约束
alter table index_tb nocheck constraint ck_index
--恢复表约束
alter table index_tb check constraint ck_index
--禁止和恢复所有check-all关键字
select 'alter table '+name+' nocheck or check constraint all' from sysobjects where type='U'
--添加新默认值和约束
ALTER TABLE index_tb ADD CONSTRAINT de_name  DEFAULT ('11111') FOR name
--删除表约束
alter table index_tb drop constraint de_name
-- 字段  UNIQUE NOT NULL = PRIMARY KEY 差不多

  
insert into index_tb(name,xb) select '12345','mm'

select * from index_tb

delete index_tb

------解决方案--------------------
NewsList.NewsTypeid 建 NONCLUSTERED 索引
NewsType.Newstypeid 建 Unique CLUSTERED索引
------解决方案--------------------
SQL code
create clustered index id_index1 on NewList(NewsTypeid)
go
create index id_index2 on NewsType(Newstypeid)
go

Select Articletitle,ArticleUrl,TypeName,NewsDate   
From NewsList,NewsType   
where NewsList.NewsTypeid=NewsType.Newstypeid

------解决方案--------------------
小梁的收藏太多了,都是好东西