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 8891497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:45:43+00:00 2026-06-14T22:45:43+00:00

select * from ( select * from y where x=t) where z rlike (.*)query1

  • 0
select * from ( select * from y where x="t") where z rlike "(.*)query1"
union
select * from ( select * from y where x="t") where z rlike "(.*)query2"

As you can see above, parenthesis contain the same query which typically
returns multi-row results. Is there any way to optimize it by only querying once for the select statement inside the parenthesis and using the result over and over again?

PS: I want to get the results in an ordered manner, so the results of the first unioned query must stay at top.

  • 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-14T22:45:44+00:00Added an answer on June 14, 2026 at 10:45 pm

    Three things here:

    • First, avoid UNION if you can use UNION ALL. Plain unions involve removal of duplicates and thus take more time.
    • When you can factor a query, do it. Here you have union of exact queries except for one criterion, then you can use OR.
    • select * from (select * ... is redundancy and can be avoided.

    Which gives:

    SELECT *
      FROM y
     WHERE x = 't'
       AND ( z RLIKE '(.*)query1'
            OR
             z RLIKE '(.*)query2'
           )
    

    As said in a comment by melpomene (thanks!) you can even factor at the regex level:
    WHERE z RLIKE '(.*)query1|(.*)query2'

    If there are duplicates in y and you don’t want them, since UNION discarded them, you can introduce DISTINCT but avoid if it is not necessary.

    If you want an order, never rely on the DBMS. Use ORDER BY:

    ORDER BY CASE WHEN z RLIKE '(.*)query1' THEN 1
                  WHEN z RLIKE '(.*)query2' THEN 2
                  ELSE 3 -- given the WHERE clause, should never happen
             END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SELECT * FROM [makes$] WHERE Corporate Name='Champion Enterprises, Inc.' I'm running this query on
SELECT * FROM Customer WHERE Name 'LIKE [a-f]%' How Can I acheive this in
select * from ( select add_months(sysdate,-2)dt from dual union select add_months(sysdate,-1) from dual union
SELECT * FROM dbname WHERE text = 'a%' (this can get all the `text`
$res = $db->prepare(SELECT user_id FROM sam_users WHERE user_name RLIKE ?); $res->execute(array($login.'[[:digit:]]*')); I need a
select * from table where key='çmyk' when i run this query on table that
SELECT * From `users` AS `User` LEFT JOIN `selections` AS `Selections` ON (`Selections`.`user_id` =
SELECT * FROM list WHERE list_item LIKE %_1375_% returns results it should not. For
SELECT * FROM tbl_transaction t LEFT JOIN tbl_transaction_hsbc ht ON t.transactionid = ht.transactionid transactionid
select * from FOO.MBR_DETAILS where BAR= 'BAZ' and MBR_No = '123' execution time =

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.