sqlschool.gr logo

articles

Articles of SQLschool.gr Team

How to find if a table has rows

Antonios Chatzipavlis
Wednesday 28 November 2018

Πολλές φορές όταν γράφουμε κώδικα είτε μέσα σε stored procedures είτε σε scripts να χρειάζεται να κάνουμε διάφορους ελέγχους. Ένας συνηθισμένος έλεγχος είναι να κοιτάμε αν υπάρχουν εγγραφές σε ένα πίνακα για να προχωρήσουμε στο επόμενο βήμα.

Συνήθως για κάτι τέτοιο γράφουμε τον παρακάτω κώδικα.

declare @c int=0;
select @c=count(*)
from dbo.T
where col1==N'xxxxxx';

if @c>0 
print 'Has records';

Το πρόβλημα με το κώδικα αυτό είναι ότι αν ο πίνακας μας έχει αρκετά μεγάλο αριθμό εγγραφών θα πρέπει είτε ο πίνακας να γίνει table scan ή αν έχουμε index που μπορεί να χρησιμοποιηθεί να έχουμε index scan.

Αυτό έχει σαν αποτέλεσμα μεγάλους χρόνους εκτέλεσης αλλά και δαπανηρά πλάνα εκτέλεσης όπως φαίνεται παρακάτω αν εκτελέσω κάτι αντίστοιχο σε ένα παραγωγικό περιβάλλον.

declare @c int=0;
select @c=count(*)
from dbo.T
where col1=N'xxxxxx';

if @c>0 
print 'Has records';

img1

Using EXISTS

Υπάρχει όμως τρόπος όλα αυτά να τα κάνουμε σχεδόν μηδέν και ο τρόπος είναι να χρησιμοποιήσουμε την EXISTS όπως παρακάτω

if exists(
select *
from dbo.T
where col1=N'xxxxxx'
   )
print 'Has records'
SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.
Table 'T Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.


img2

Η εξήγηση είναι απλή. Η EXISTS σταματάει μόλις βρει το πρώτο record που την ικανοποιεί.

Using TOP(1)

Στο σημείο αυτό θα πρέπει να επισημάνω ότι σε κάποιες περιπτώσεις (που θα πρέπει για να τις βρείτε να πειραματιστείτε) να δείτε την ίδια συμπεριφορά με την EXISTS χρησιμοποιώντας την TOP(1) αλλά θεωρώ ότι η χρήση της EXISTS είναι καλύτερη καθώς γράφω και λιγότερο κώδικα και γλυτώνω την χρήση της μεταβλητής.


//antonch


Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

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-2025 All rights reserved

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