Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Простой "детский" вопрос по индексам / 3 сообщений из 3, страница 1 из 1
05.11.2002, 13:07:41
    #32064931
Label
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Простой "детский" вопрос по индексам
Вопрос скорее по теории. Дело в том, что об индексах я знаю только то, что "их надо использовать везде, на тех стобцах, по которым будет происходить наиболее частый отбор" (если что не так- не пеняйте, лучше ссылкой.. :-)
Есть таблица по типу:

Код: plaintext
1.
2.
3.
4.
5.
CREATE TABLE OrganClass (
	[organId]     int NOT NULL REFERENCES Organization([organId]),
	[ClassLev1Id] int NOT NULL REFERENCES ClassLev1([ClassLev1Id]),
	[ClassLev2Id] int NOT NULL REFERENCES ClassLev2([ClassLev2Id])
) ON [PRIMARY]
GO

Таблица практически не будет ни INSERT-иться ни UPDATE-еться, то есть служить исключительно для выборки.
При чем запросы будут идти по всем столбцам:
Код: plaintext
1.
2.
.. WHERE [organId]=@p_what
.. WHERE [ClassLev1Id]=@p_what
.. WHERE [ClassLev2Id]=@p_what


Вопрос: есть ли смысл создавать индексы на каждый из столбцов с целью ускорить выбрку?
Заранее спасибо.
...
Рейтинг: 0 / 0
05.11.2002, 13:14:53
    #32064934
antonz
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Простой "детский" вопрос по индексам
Если в таблице больше нескосльких тычяч записей, то имеет смысл использовать индексы.
...
Рейтинг: 0 / 0
05.11.2002, 13:39:35
    #32064942
vics
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Простой "детский" вопрос по индексам
Avoid using variables in the WHERE clause of a query located in a batch file. Let's find out why this may not be a good idea.

First, let's look at the following code:

SELECT employee_id
FROM employees
WHERE age = 30 and service_years = 10

Assuming that both the age and the service_years columns have indexes, and the table has many thousands of records, then SQL Server's Query Optimizer will select the indexes to perform the query and return results very quickly.

Now, let's look at the same query, but written to be more generic, one that you might find in a generic batch file:

DECLARE @age int
SET @age = "30"
DECLARE @service_years int
SET @service_years = "10"
SELECT employee_id
FROM employees
WHERE age = @age and service_years = @service_years

When the above code is run, even though both the age and the service_years columns have indexes, they may not be used, and a table scan may be used instead, potentially greatly increasing the amount of time for the query to run.

The reason the indexes may not be used is because the Query Analyzer does not know the value of the variables when it selects an access method to perform the query. Because this is a batch file, only one pass is made of the Transact-SQL code, preventing the Query Optimizer from knowing what it needs to know in order to select an access method that uses the indexes.

If you cannot avoid using variables in the WHERE clauses of batch scripts, consider using an INDEX query hint to tell the Query Optimizer to use the available indexes instead of ignoring them and performing a table scan. This of course that the indexes are highly selective. If the indexes are not highly selective, then a table scan most likely be more efficient than using the available indexes.

Another option is to not use a script, but a stored procedure instead. Variables in stored procedures don't cause the problem described above.
...
Рейтинг: 0 / 0
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Простой "детский" вопрос по индексам / 3 сообщений из 3, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]