In this article, we will see how to improve the performance of such queries, with the help of an example. We will optimize SQL queries using UNION operator instead of OR
Let’s say we have a table to store customer information. That table has five columns viz ( id, Name, Country, Age, Total_Spending ).
Following is the SQL to create customer info table.
CREATE TABLE CustomerInfo( cust_id NUMBER NOT NULL PRIMARY KEY,
cust_name CHAR(50), country CHAR(4), age NUMBER(3), total_spending NUMBER );
Now, load some data into it. Following is the SQL for the same. I have loaded near about hundred records in CustometInfo Table.
INSERT INTO CustomerInfo VALUES ( 1, 'James Smith', 'USA', 23, 5800);
Next step is to create indices for all the columns of CustomerInfo table.
CREATE INDEX cust_name_i1 ON CustomerInfo (cust_name);
CREATE INDEX cust_country_i2 ON CustomerInfo (country);
CREATE INDEX cust_age_i3 ON CustomerInfo (age);
CREATE INDEX cust_spending_i4 ON CustomerInfo (total_spending);