Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:23:08+00:00 2026-06-12T00:23:08+00:00

I have the following query, which fetches data from only one table. EDIT: It

  • 0

I have the following query, which fetches data from only one table.

EDIT:
It is a query which is supposed to return data for an autocomplete function.

  1. Autocomplete data can be in text1 or text2.
  2. The exact matches should be on top. int3 is an integer weight value, where the order of results are based on this. First two queries are here to identify exact matches.
  3. The next two queries are here for the identify near matches. WHERE text1 > 'foo' AND text1 < 'fop' phrase is actually equal to WHERE text1 LIKE 'foo%'. I wrote it like this to benefit from index.

Hope this helps.

SELECT DISTINCT text1 as Key, 'text1' as Source, int1 as Count, 1000 as int3 
   FROM mytable 
   WHERE text1 = 'foo' 

UNION SELECT DISTINCT text2 as Key, 'text2' as Source, int2 as Count, 1000 as int3 
   FROM mytable 
   WHERE text2 = 'foo' 

UNION SELECT text1 as Key, 'text1' as Source, int1 as Count, MAX(int3) as int3 
   FROM mytable 
   WHERE text1 > 'foo' AND text1 < 'fop' AND Count < 4 
   GROUP BY Key 

UNION SELECT text2 as Key, 'text2' as Source, int2 as Count, MAX(int3) as int3 
   FROM mytable 
   WHERE text2 > 'foo' AND text2 < 'fop' AND Count < 4 
   GROUP BY Key 

ORDER BY int3 DESC, Count, Key LIMIT 0, 15;

The table structure is:

CREATE TABLE mytable (text1 TEXT, text2 TEXT, 
                      int1 NUMERIC, int2 NUMERIC, int3 NUMERIC);

It works fine, but I need to fine tune the performance. I tried different indexing options and checked the performance results with built in timer.

The best indexing options I discovered are :

CREATE INDEX cmp1 ON mytable (text1 ASC, int1 ASC, int3 DESC);
CREATE INDEX cmp2 ON mytable (text2 ASC, int2 ASC, int3 DESC);

I will be glad if you could show me any better indexing option or a better performing SQL query.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:23:09+00:00Added an answer on June 12, 2026 at 12:23 am

    UNION removes duplicate rows that appear in both subqueries, and therefore must create a temporary table for the result.

    If you can guarantee that the subqueries are distinct, or if you don’t care about those duplicates, use UNION ALL instead.

    (In your case, the sorting by the non-indexed value int3 requires a temporary table anyway.)


    To optimize the subqueries, run them with EXPLAIN QUERY PLAN to check whether they use indexes.

    You can use LIKE if you ensure that your indexes are declared correctly; see LIKE optimization.

    See Query Planning for an explanation of how to construct indexes. Your two indexes seem to be optimal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query which obtains transactions from the transactions table and transaction
I have the following query which have 1000 rows select staffdiscountstartdate,datediff(day,groupstartdate,staffdiscountstartdate), EmployeeID from tblEmployees
I have a sql table which have the following data, Id City Country ---
I have the following query which is takin 3 seconds on a table of
I have the following query which should return no rows, but returns 4: var
I have the following query which is supposed to be selecting all the articles
I have following table which saves sales data for specific week of a year.
I have the following query (which should return a scalar/numeric), But I still get
I have the following query which is returning 4 results when the table FI_CurrentReceiptData
I have the following query which I use to return the highest scores in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.