① 如何重建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
要不就根據需要寫個存儲過程,沒有直接的方法