powered by simpleCommunicator - 2.0.49     © 2025 Programmizd 02
Форумы / Sybase ASA, ASE, IQ [игнор отключен] [закрыт для гостей] / ASE 15.5 стоимость поиска значения или поиска if exist
8 сообщений из 8, страница 1 из 1
ASE 15.5 стоимость поиска значения или поиска if exist
    #38438650
blzz
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
-- тестовая среда Adaptive Server Enterprise/15.5/EBF 20636 SMP ESD#5.2/P/x86_64/Enterprise Linux/asear155/2602/64-bit/FBO/Sun Dec  9 15:11:04 2012

create table tablename (
id unsigned int identity not null,
f1 varchar(32),
f2 varchar(40) NULL,
f3 char(1)
)

set nocount on 
go 
declare @val unsigned int 
select @val=convert(unsigned int, next_identity('tablename')) 
insert into tablename values (
	hash(@val,'md5'),
	hash(@val,'sha1'),
convert(char(1),convert(int,floor(5*rand() )))
)
go 5000

set nocount off 
go 
create unique nonclustered index IND on tablename ( f1 ASC, f2 ASC, f3 ASC )
go



есть необходимость проверять существование (или несуществование) записи с определенными условиями:
Код: sql
1.
 if exists ( select 1 from tablename where f1=@f1 and f2=@f2 and f3='0')  



и также аналогично проверять существование условий но если запись есть - также получить её id
Код: sql
1.
2.
  select @ruleid=id from tablename where f1=@f1 and f2=@f2 and f3='1' 
   if @@rowcount >0 




но вот стоимости планов меня настораживают:

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
/* у меня есть записи
        3361 04488aeba7ce50882aa3668496408543 7f8d12ea4c143eed43e968aa87c02a97fc6a8256 0    
        3479 f587eed3302aca2fc07f425054424225 b9f101f13da4facf1c4bb3e44d740d10929e1748 1    
        3490 ec6c3ff393d7b8cde23e2aa57a1e1154 70d4e0f7388dee71993a3e85c6a6ef25d78c7995 1    
*/

if exists ( select 1 from tablename where f1='04488aeba7ce50882aa366849640854' and f2='7f8d12ea4c143eed43e968aa87c02a97fc6a8256' and f3='0')  print "FOUND 0"
go 


план:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
QUERY PLAN FOR STATEMENT 1 (at line 1). 
Optimized using Serial Mode 

    STEP 1 
        The type of query is COND. 


	4 operator(s) under root 


       |ROOT:EMIT Operator (VA = 4) 
       | 
       |   |SQFILTER Operator (VA = 3) has 2 children. 
       |   | 
       |   |   |SCALAR Operator (VA = 0)  
       |   | 
       |   |  Run subquery 1 (at nesting level 1). 
       |   | 
       |   |  QUERY PLAN FOR SUBQUERY 1 (at nesting level 1 and at line 1). 
       |   | 
       |   |   Non-correlated Subquery. 
       |   |   Subquery under an EXISTS predicate. 
       |   | 
       |   |   |SCALAR AGGREGATE Operator (VA = 2) 
       |   |   |  Evaluate Ungrouped ANY AGGREGATE. 
       |   |   |  Scanning only up to the first qualifying row. 
       |   |   | 
       |   |   |   |SCAN Operator (VA = 1) 
       |   |   |   |  FROM TABLE 
       |   |   |   |  tablename 
       |   |   |   |  Index : IND 
       |   |   |   |  Forward Scan. 
       |   |   |   |  Positioning by key. 
       |   |   |   |  Index contains all needed columns. Base table will not be read. 
       |   |   |   |  Keys are: 
       |   |   |   |    f1 ASC 
       |   |   |   |    f2 ASC 
       |   |   |   |    f3 ASC 
       |   |   |   |  Using I/O Size 8 Kbytes for index leaf pages. 
       |   |   |   |  With LRU Buffer Replacement Strategy for index leaf pages. 
       |   | 
       |   |  END OF QUERY PLAN FOR SUBQUERY 1. 




Server Message:  Number  3630, Severity  10
Server 'Meridian', Line 0:
Total estimated I/O cost for statement 1 (at line 1): 19149. 
19тыс попугаев.

поиск записи по индексу - 81 попугай и одно попугайское крылышко.
Код: sql
1.
2.
3.
4.
declare @id unsigned int 
select @id=id from tablename where f1='ec6c3ff393d7b8cde23e2aa57a1e1154' and f2='70d4e0f7388dee71993a3e85c6a6ef25d78c7995' and f3='1' 
   if @@rowcount >0 print "FOUND: %1!", @id
go 


план:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
QUERY PLAN FOR STATEMENT 2 (at line 2). 
Optimized using Serial Mode 
    STEP 1 
        The type of query is SELECT. 


	1 operator(s) under root 


       |ROOT:EMIT Operator (VA = 1) 
       | 
       |   |SCAN Operator (VA = 0) 
       |   |  FROM TABLE 
       |   |  tablename 
       |   |  Index : IND 
       |   |  Forward Scan. 
       |   |  Positioning by key. 
       |   |  Keys are: 
       |   |    f1 ASC 
       |   |    f2 ASC 
       |   |    f3 ASC 
       |   |  Using I/O Size 8 Kbytes for index leaf pages. 
       |   |  With LRU Buffer Replacement Strategy for index leaf pages. 
       |   |  Using I/O Size 8 Kbytes for data pages. 
       |   |  With LRU Buffer Replacement Strategy for data pages. 




Server Message:  Number  3630, Severity  10
Server 'Meridian', Line 0:
Total estimated I/O cost for statement 2 (at line 2): 81. 


как-то странно, что exists - лишь поиск по индексу, в то время как поиск по индексу требует также доступа в таблицу, но оценка лучше.
...
Рейтинг: 0 / 0
ASE 15.5 стоимость поиска значения или поиска if exist
    #38438930
Фотография MasterZiv
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
blzz,

Планы -то одинаковые.
...
Рейтинг: 0 / 0
ASE 15.5 стоимость поиска значения или поиска if exist
    #38438941
blzz,

А попробуйте заменить 1 на id.
if exists ( select id from tablename where f1='04488aeba7ce50882aa366849640854' and f2='7f8d12ea4c143eed43e968aa87c02a97fc6a8256' and f3='0') print "FOUND 0"
go
...
Рейтинг: 0 / 0
ASE 15.5 стоимость поиска значения или поиска if exist
    #38439431
blzz
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
забыл пароль,
тоже под 20 тыс.
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
       |ROOT:EMIT Operator (VA = 4) 
       | 
       |   |SQFILTER Operator (VA = 3) has 2 children. 
       |   | 
       |   |   |SCALAR Operator (VA = 0)  
       |   | 
       |   |  Run subquery 1 (at nesting level 1). 
       |   | 
       |   |  QUERY PLAN FOR SUBQUERY 1 (at nesting level 1 and at line 1). 
       |   | 
       |   |   Non-correlated Subquery. 
       |   |   Subquery under an EXISTS predicate. 
       |   | 
       |   |   |SCALAR AGGREGATE Operator (VA = 2) 
       |   |   |  Evaluate Ungrouped ANY AGGREGATE. 
       |   |   |  Scanning only up to the first qualifying row. 
       |   |   | 
       |   |   |   |SCAN Operator (VA = 1) 
       |   |   |   |  FROM TABLE 
       |   |   |   |  tablename 
       |   |   |   |  Index : IND 
       |   |   |   |  Forward Scan. 
       |   |   |   |  Positioning by key. 
       |   |   |   |  Keys are: 
       |   |   |   |    f1 ASC 
       |   |   |   |    f2 ASC 
       |   |   |   |    f3 ASC 
       |   |   |   |  Using I/O Size 8 Kbytes for index leaf pages. 
       |   |   |   |  With LRU Buffer Replacement Strategy for index leaf pages. 
       |   |   |   |  Using I/O Size 8 Kbytes for data pages. 
       |   |   |   |  With LRU Buffer Replacement Strategy for data pages. 
       |   | 
       |   |  END OF QUERY PLAN FOR SUBQUERY 1. 

Total estimated I/O cost for statement 1 (at line 1): 27402. 
...
Рейтинг: 0 / 0
ASE 15.5 стоимость поиска значения или поиска if exist
    #38439559
cherrex_Den
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
blzz,

Покажите что покажет set statistics io on при этих двух запросах!
...
Рейтинг: 0 / 0
ASE 15.5 стоимость поиска значения или поиска if exist
    #38439892
blzz
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
cherrex_Den,

а так всё логично:

if exists
Код: plaintext
1.
2.
3.
4.
Table: tablename scan count 1, logical reads: (regular=2 apf=0 total=2), physical reads: (regular=0 apf=0 total=0), apf IOs used=0 
Total actual I/O cost for this command: 4. 
Total writes for this command: 0 
Total actual I/O cost for this command: 0. 
Total writes for this command: 0 

select id from
Код: plaintext
1.
2.
Table: tablename scan count 1, logical reads: (regular=3 apf=0 total=3), physical reads: (regular=0 apf=0 total=0), apf IOs used=0 
Total actual I/O cost for this command: 6. 
Total writes for this command: 0 
...
Рейтинг: 0 / 0
ASE 15.5 стоимость поиска значения или поиска if exist
    #38439925
cherrex_Den
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
blzz,

И не парьтесь!
Для успокоение, можно посмотреть

Код: sql
1.
2.
dbcc traceon(3604)
set option show_lio_costing brief



FINAL PLAN ( total cost = .......)
...
Рейтинг: 0 / 0
ASE 15.5 стоимость поиска значения или поиска if exist
    #38440208
Лучше так (IMHO):

set statistics plancost on

select ...
...
Рейтинг: 0 / 0
8 сообщений из 8, страница 1 из 1
Форумы / Sybase ASA, ASE, IQ [игнор отключен] [закрыт для гостей] / ASE 15.5 стоимость поиска значения или поиска if exist
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


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