我表中数据超过百万条记录,如何快速查询?
[dbo].[sb] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[code] [numeric](18, 0) NULL ,
[name] [nvarchar] (1500) NULL ,
[num] [nvarchar] (50) NULL ,
[hetong] [numeric](18, 0) NULL ,
[inda] [smalldatetime] NULL ,
[da] [datetime] NOT NULL ,
[used] [int] NOT NULL 

解决方案 »

  1.   

    --索引资料
    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_tbdelete index_tb
      

  2.   

    想要实现快速查询,要注意你的查询语句确实使用主键进行查询,即where t.pkey = 某值,并且对主键不能用函数计算,或者用大于小于比较,否则查询的时候不会用主键进行查询,而是全表查询。
    另外,可以考虑将你的表分类成多表存放,可能对业务有影响,不推荐