jeudi 30 juin 2016

For Sql performances, several equals or one between

For a new developement, I will have a big SQL table (~100M rows). 4 fields will be used to query the data.

Is it better to query one concatenated field with between or several equals ?

Exemple :

MainTable

PkId | Label | FkId1 | FkId2 | FkId3 | FkId4
1    | test  | 1     | 4     | 3     | 1

Datas in Fk tables are static, example :

FkTable1

Id | Value
1  | a
2  | b
3  | c 

To query the datas, the classic sql query is :

select Label, FkId1, FkId2, FkId3, FkId4 
from MainTable
where FkId1=1 and FkId2=2 and FkId3 in(2, 3)

The idea to optimize performance is to add one field "UniqueId" calculated backend before the insert :

UniqueId = FkId1*1000000 + FkId2*10000 + FkId3*100 + FkId4
PkId | Label | FkId1 | FkId2 | FkId3 | FkId4 | UniqueId
1    | test  | 1     | 4     | 3     | 1     | 1040301
select Label, FkId1, FkId2, FkId3, FkId4 
from MainTable
where UniqueId between 1020200 and 1040000

Moreover, with the UniqueId field, an index on this field only will be sufficient.

What do you think ?

Thanks

Aucun commentaire:

Enregistrer un commentaire