sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Unique Indexes Vs Filter Indexes

Antonios Chatzipavlis
Monday 16 June 2014

Σε ένα προϊόν σαν τον SQL Server που είναι περίπλοκο και  περιέχει αρκετή περιπτωσιολογία είναι φυσικό όσοι ασχολούνται περιστασιακά ή επιφανειακά μαζί του να μπερδεύουν κάποια πράγματα και να βγάζουν λάθος συμπεράσματα. Σε αυτή την περίπτωση καλό θα είναι να μπαίνουν όλα σε μια σειρά.

Πριν μερικές μέρες βρέθηκα σε μια συζήτηση  στην οποία ένας  συνάδελφος ανέφερε ότι από τον SQL Server 2008 και μετά ο SQL Server επιτρέπει σε ένα unique index να έχει περισσότερα από ένα NULL key values. Εντυπωσιάστηκα και ταυτόχρονα αναρωτήθηκα πως μου ξέφυγε αυτό. Φυσικά ρώτησα τον συνάδελφο για το τι ακριβώς εννοούσε ώστε να αποκλείσω οποιαδήποτε παρανόηση. Ο συνάδελφος μου είπε ότι ακριβώς ανέφερα και παραπάνω, φυσικά τον ρώτησα πως το κάνει αυτό και μου απάντησε με χαρά ότι το κάνει με filter index. 

Οκ! Ας βάλουμε λίγο τα πράγματα σε μια σειρά.

Από τα BOL και συγκεκριμένα από το topic για το πώς μπορεί κάποιος να δημιουργήσει ένα Unique Index (http://msdn.microsoft.com/en-us/library/ms187019.aspx) αντιγράφω την χαρακτηριστική φράση
"A unique index guarantees that the index key contains no duplicate values and therefore every row in the table is in some way unique", να συμπληρώσω ότι αυτό αφορά και το NULL και έτσι μπορείς να έχεις μόνο μια εγγραφή που να έχει αυτή την τιμή στο πεδίο που έχεις σαν unique index.

Από τα BOL και συγκεκριμένα από το topic για τους Filtered Indexes (http://msdn.microsoft.com/en-us/library/cc280372.aspx) αντιγράφω
"A filtered index is an optimized nonclustered index especially suited to cover queries that select from a well-defined subset of data. It uses a filter predicate to index a portion of rows in the table. A well-designed filtered index can improve query performance as well as reduce index maintenance and storage costs compared with full-table indexes"

Φυσικά και μπορείς να έχεις filtered unique index στον οποίο να μην συμπεριλαμβάνονται NULL values και έτσι να μπορείς να ξεπερνάς αυτό που θεωρούσες σαν εμπόδιο στον unique index, αλλά είναι δύο διαφορετικά πράγματα και ανάλογα με την περίπτωση μπορείς να επιλέξεις τι θα χρησιμοποιήσεις.

Πρώτα από όλα θα πρέπει να απαντήσεις στο ερώτημα γιατί θέλεις την μοναδικότητα και αφού ζητάς αυτή γιατί επιτρέπεις να έχεις περισσότερα από ένα NULL key values, που πάει η μοναδικότητα τότε;
Από την άλλη θα πρέπει να καταλάβεις ότι όταν θα ψάχνεις να βρεις αυτά που έχουν null value δεν θα χρησιμοποιηθεί ο filter index που έχεις φτιάξει.

Άρα μιλάμε για δύο διαφορετικά features που σε καμία περίπτωση το ένα δεν ακυρώνει το άλλο ούτε συμπληρώνει το ένα το άλλο. Η επιχειρηματική σου λογική είναι αυτή που θα σε οδηγήσει στο τι θα χρησιμοποιήσεις και φυσικά οι παραδοχές που θα πρέπει να έχεις πάρει για το πώς θα διαβάζεις τα δεδομένα αυτά. Τέλος θα πρέπει να επισημάνω ότι αν βάλεις unique constraint αυτό ακολουθεί την λογική του unique index

Για όσους θέλουν αν πειραματιστούν ένα μικρό παράδειγμα.

create table N
(
      id int
    , col1 nvarchar(10) null
);
go

create unique nonclustered index UI_N_col1 on N(col1);
go

insert into N values (1,'antonis');
insert into N values (2,'mitsos');
insert into N values (3,null);
insert into N values (4,null); -- error
go

select * from N;
go

create table M
(
      id int
    , col1 nvarchar(10) null
);
go

create unique nonclustered index UI_M_col1 on M(col1)
where col1 is not null;
go

insert into N values (1,'antonis');
insert into N values (2,'mitsos');
insert into N values (3,null);
insert into N values (4,null);
go

select * from M;
go

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios is a Data Solutions Consultant and Trainer. He has been working in IT since 1988. In his career, he has worked as senior developer, IT Manager, Solutions Architect and IT Consultant. Since 1995 he has been devoted on new technologies and software development tools, mainly by Microsoft, either by training company staff and colleagues or assisting them in design, development and implementation as a consultant or chief developer. He has focused in Databases and Data Science since 1995. He specialized in Microsoft SQL Server since version 6.0 in areas like SQL Server Internals, Database Design and Development, Business Intelligence and in 2010 he has started working with Azure Data Platform, NoSQL databases, Big Data Technologies and Machine Learning. He is an active member of many IT communities in Greece, answering colleagues' questions and writing articles in his web site. He is the owner of SQLschool.gr which is a community portal with a lot of information about Microsoft SQL Server. He has been a Microsoft Certified Trainer (MCT) since 2000. Microsoft honored him as MVP on Data Platform due to his activities in SQL Server since 2010. He holds a large number of Microsoft Certifications and Microsoft SQL Server Certifications since version 6.5.

Episode

Working with Delta Lake tables in Microsoft Fabric

image

More Episodes...

Tip

Refresh Intellisence in SSMS

Για να κάνουμε refresh το intellisence μέσα στο SSMS αρκεί να πατήσουμε Ctrl+Shift+R

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn


sqlschool.gr © 2010-2023 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.