Query Performance Comparison

Query Performance Comparison

What is faster: two LIKE statements or one BETWEEN?

((MyData LIKE '02105_') OR ((MyData LIKE '02106_)MyData BETWEEN '021050' AND '021069'

Please advise.

When trying to decide which is the faster SQL for a particular query, my philosophy is to test the two options rather than making an educated guess.

Using version 7, I created a table with 100,000 rows with data similar to your example. Then I turned on show stats i/o and ran the two versions you provided. There was absolutely no difference in performance. That makes sense because, in both cases, the optimizer took advantage of an index on that column.

However, you should realize that your queries are not exactly the same. The LIKE keyword will return a match on the value ‘02105’, while the query using the BETWEEN statement will skip that row.

Here is the code I used to test your queries:

use pubsgoset nocount ongoif exists(select * from sysobjects where type = 'U' and name = 'liketest')   drop table liketestgocreate table liketest(   i char(6) constraint pk_liketest        primary key (i))godeclare @i intselect @i = 0while @i < 100000begin     insert into liketest (i) values ( convert(char,(floor(rand(@i)* 1000000)),0))         select @i = @i + 1endselect * fromliketest wherei LIKE '12105_' OR i LIKE '12106_'select *from liketest wherei BETWEEN '121050' AND '121069'

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing