当前位置:首页 » 编程语言 » sql修复索引
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql修复索引

发布时间: 2022-04-17 19:23:26

① 如何重建sql索引 要具体的命令

USE TableName
DECLARE @TableName varchar(255)
DECLARE TableCursor CURSOR FOR
SELECT table_name FROM information_schema.tables
WHERE table_type = 'base table'
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
DBCC DBREINDEX(@TableName,' ',90)
FETCH NEXT FROM TableCursor INTO @TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor

② SQL中如何重建一张表的索引

SELECT
tab.name AS [表名],
idx.name AS [索引名称],
col.name AS [列名]
FROM
sys.indexes idx
JOIN sys.index_columns idxCol
ON (idx.object_id = idxCol.object_id
AND idx.index_id = idxCol.index_id
)
JOIN sys.tables tab
ON (idx.object_id = tab.object_id)
JOIN sys.columns col
ON (idx.object_id = col.object_id
AND idxCol.column_id = col.column_id);

③ 如何在 SQL 数据库优化 索引,SQL索引优化

1、主键就是聚集索引
2、只要建立索引就能显着提高查询速度
3、把所有需要提高查询速度的字段都加进聚集索引,以提高查询速度

(四)其他书上没有的索引使用经验总结
1、用聚合索引比用不是聚合索引的主键速度快
2、用聚合索引比用一般的主键作order by时速度快,特别是在小数据量情况下
3、使用聚合索引内的时间段,搜索时间会按数据占整个数据表的百分比成比例减少,而无论聚合索引使用了多少个
4 、日期列不会因为有分秒的输入而减慢查询速度

(五)其他注意事项
1. 不要索引常用的小型表
2. 不要把社会保障号码(SSN)或身份证号码(ID)选作键
3. 不要用用户的键
4. 不要索引 memo/notes 字段和不要索引大型文本字段(许多字符)
5. 使用系统生成的主键

二、改善SQL语句
1、Like语句是否属于SARG取决于所使用的通配符的类型
2、or 会引起全表扫描
3、非操作符、函数引起的不满足SARG形式的语句
4、IN 的作用相当与OR
5、尽量少用NOT
6、exists 和 in 的执行效率是一样的
7、用函数charindex()和前面加通配符%的LIKE执行效率一样
8、union并不绝对比or的执行效率高
9、字段提取要按照“需多少、提多少”的原则,避免“select *”
10、count(*)不比count(字段)慢
11、order by按聚集索引列排序效率最高
12、高效的TOP

④ 如何重建sql数据库索引

数据更新是一种常见的操作,然后数据仓库的概念一般要求的是数据是集成、稳定的。HIVE作为一种分布式环境下以HDFS为支撑的数据仓库,它同样更多的要求数据是不可变的。
然而现实很多任务中,往往需要对数据进行更新操作,经查,Hive自0.11版本之后就提供了更新操作。于是想着试验一下,看看HIVE更新的操作和性能。

⑤ SQL2000 主键,索引,相关性没了怎么能够恢复

select into只能复制数据,不能复制表的相关对象,如主键索引约束等
这个只能你自己在旧表中导出脚本在然后给新表执行了,2005的话可以生成这个表的创建脚本,可以从中提取主键索引等信息,2000不清楚有没有这个功能

⑥ sql问题,索引的修改。alter index语句如何使用,谢谢

alter index常用的语法如下:
(1)重建指定索引:
ALTER INDEX ind ON TA
REBUILD;
(2)重建全部索引:
ALTER INDEX ALL ON TA
REBUILD;
(3)禁用索引:
ALTER INDEX ALL ON TA
DISABLE;
(再次启用使用REBUILD重建而不是ENABLED)
(4)指定参数重建索引:
ALTER INDEX ALL ON TA
REBUILD WITH(FILLFACTOR=80);
(5)指定参数修改索引:
ALTER INDEX ALL ON TA
SET(IGNORE_DUP_KEY = ON);

注意:alter index语法,不能用于修改索引定义,如添加或删除列,或更改列的顺序

⑦ sql语句中怎么修改 索引

我觉得应该不能修改索引吧。。创建索引的目的是为了提高查询效率,是对于某个具体属性而建立的,如果用户觉得这个索引不适合,或者说这个索引创建的不好,那么可以删除索引,重新再创建一个索引。某个属性创建索引不适合,可以把该索引删掉,再在另外的属性上创建一个索引,因此应该就没有必要再设计一个可以创建索引的方法或语句。删除后再创建一个索引可以达到这样的效果。

要不你把题目仔细说一下。。有试过搭配用desc来达到降序的目的吗

⑧ SQL 数据库索引如何维护

第一步:查看是否需要维护,查看扫描密度/Scan Density是否为100%
declare @table_id int
set @table_id=object_id('表名')
dbcc showcontig(@table_id)

第二步:重构表索引
dbcc dbreindex('表名',pk_索引名,100)
重做第一步,如发现扫描密度/Scan Density还是小于100%则重构表的所有索引
dbcc dbreindex('表名','',100)

⑨ sql server如何重建索引到其它文件组

在日常工作中,我们发现很多实施案例中,sql server的数据库数据与索引在一起。我见过一个客户的,他的数据库总共大小才60g,但索引与数据完全混在一起,从管理数据库的直觉来看,性能方面肯定有问题,所以我建议他们,不管怎么样,把索引与数据库分开,对性能是有好处的!但是sql server的索引,想要通过重建的方式,把数据与索引分开,并不是一件容易的事怀,在使用rebuild时,并不能增加文件组选项。后来研究发现,可以通过以下方式把数据与非聚簇索引分开,具体如下:
set nocount on

declare @index table
(
object_id int,
objectName sysname,
index_id int,
indexName sysname,
fill_factor tinyint,
allow_row_locks bit,
allow_page_locks bit,
is_padded bit,
indexText varchar(max),
indexTextEnd varchar(max)
)

declare @indexColumn table
(
object_id int,
index_id int,
column_id int,
index_column_id int,
max_index_column_id int,
is_descending_key bit,
is_included_column bit,
columnName varchar(255),
indexText varchar(max) null
)

insert into @index
select
i.object_id,
object_name(i.object_id),
i.index_id,
i.name,
fill_factor,
allow_row_locks,
allow_page_locks,
is_padded,
'CREATE NONCLUSTERED INDEX [' + i.name + '] ON [dbo].[' + object_name(i.object_id) + '] ' + char(13),
'WITH (PAD_INDEX = ' +
CASE WHEN is_padded = 1 THEN ' ON ' ELSE ' OFF ' END +
', STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = ON, ONLINE = OFF, ALLOW_ROW_LOCKS = ' +
CASE WHEN allow_row_locks = 1 THEN ' ON ' ELSE ' OFF ' END +
', ALLOW_PAGE_LOCKS = ' +
CASE WHEN allow_page_locks = 1 THEN ' ON ' ELSE ' OFF ' END +
CASE WHEN fill_factor > 0 THEN ', FILLFACTOR = ' + convert(varchar(3), fill_factor) ELSE '' END +
') ON [IndexFG];print('''+i.name+' @ '+object_name(i.object_id)+''')' --+ CHAR(13) + ' GO;'+ CHAR(13) --注意标红的地方,这是新的文件组的名称
from sys.indexes i
where i.type = 2 and not exists(select 1 from sys.key_constraints kc where kc.name=i.name)
and objectproperty(i.object_id , 'IsUserTable') = 1
order by object_name(i.object_id), i.name

insert into @indexColumn
select
i.object_id,
i.index_id,
ic.column_id,
ic.index_column_id,
max(ic.index_column_id) over (partition by i.object_id, i.index_id, is_included_column),
is_descending_key,
is_included_column,
'[' + c.name + ']',
null
from @index i
join sys.index_columns ic
on i.object_id = ic.object_id
and i.index_id = ic.index_id
join sys.columns c
on ic.object_id = c.object_id
and ic.column_id = c.column_id
order by i.object_id, i.index_id, ic.index_column_id

declare @fields varchar(max)
declare @object_id int, @index_id int

select @fields = null, @object_id = -1, @index_id = -1

update @indexColumn
set @fields = indexText =
case when object_id = isnull(@object_id, object_id) and index_id = isnull(@index_id, index_id)
then isnull(@fields + ', ', ' ') + columnName + case when is_descending_key = 0 then ' ASC' else ' DESC' end
else columnName + case when is_descending_key = 0 then ' ASC' else ' DESC' end
end,
@object_id = case when object_id <> @object_id
then object_id else @object_id end,
@index_id = case when index_id <> @index_id
then index_id else @index_id end
from @indexColumn
where is_included_column = 0

select @fields = null, @object_id = -1, @index_id = -1

update @indexColumn
set @fields = indexText =
case when object_id = isnull(@object_id, object_id) and index_id = isnull(@index_id, index_id)
then isnull(@fields + ', ', ' ') + columnName
else columnName
end,
@object_id = case when object_id <> @object_id
then object_id else @object_id end,
@index_id = case when index_id <> @index_id
then index_id else @index_id end
from @indexColumn
where is_included_column = 1

update @index
set indexText = i.indexText + '( ' + char(13) + char(9) + ic.indexText + char(13) + ') '
from @index i join @indexColumn ic
on i.object_id = ic.object_id
and i.index_id = ic.index_id
and ic.index_column_id = ic.max_index_column_id
and ic.is_included_column = 0

update @index
set indexText = i.indexText + 'INCLUDE ( ' + char(13) + char(9) + ic.indexText + char(13) + ') '
from @index i join @indexColumn ic
on i.object_id = ic.object_id
and i.index_id = ic.index_id
and ic.index_column_id = ic.max_index_column_id
and ic.is_included_column = 1

update @index
set indexText = indexText + indexTextEnd
from @index

select indexText, objectName, indexName
from @index

最后的查询结果第一行就是执行的命令!

⑩ SQL 修改 索引

select num from table num <=2
union all
select num from table num = 6
union all
select num from table num >=3 and num <> 6

要不就根据需要写个存储过程,没有直接的方法