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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:57:40+00:00 2026-05-23T18:57:40+00:00

I have a VIEW which is SLOW. I don’t like the VIEW Statements as

  • 0

I have a VIEW which is SLOW. I don’t like the VIEW Statements as there are lot of JOINS and UNION.

Here is the view statement.

Create VIEW NewView AS

SELECT t2.* FROM Table1 t1
JOIN Table2 t2
ON t1.Column1 = t2.Column1 AND t1.Column2 = t2.Column2
WHERE t1.Column3 !='String'

UNION

SELECT t1.*, 'Add this string to the Last Column' FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.Column1 = t2.Column1 AND t1.Column2 = t2.Column2
WHERE t2.Column1 is null OR t1.Column3 ='String'
ORDER BY Column 4

Basically the idea is if a record exists in Table1 and Table2, the record from Table2 should overlay the record from Table1. How can I optimize this?

I have a primary key id INT NOT NULL AUTO_INCREMENT PRIMARY KEY in both the tables but I am not sure how I can integrate that with the view. I want the view to have a primary key or composite key. I cannot use other columns as all the columns can have null and duplicate values.

  • 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-05-23T18:57:41+00:00Added an answer on May 23, 2026 at 6:57 pm

    You can join and compare, using an outer join, then use COALESCE to prefer T2 to T1.

    To retain a unique key, and assuming your ID’s are all positive, you can make one table’s id’s negative.

    SELECT
      COALESCE(t2.id,     -t1.id)      AS id,
      COALESCE(t2.Column1, t1.Column1) AS Column1,
      COALESCE(t2.Column2, t1.Column2) AS Column2
    FROM
      Table1          AS t1
    FULL OUTER JOIN
      Table2          AS t2
        ON  t1.Column1 = t2.Column2
        AND t1.Column2 = t2.Column2
    WHERE
      COALESCE(t2.Column3, t1.Column3) = 'String'
    

    EDIT:

    For more complex rules on selecting which table has precidence, you can just use CASE statements…

    (This does the same as above, but can be modified for different precidence rules.)

    SELECT
      CASE WHEN t2.id IS NULL THEN -t1.id      ELSE t2.id      END  AS id,
      CASE WHEN t2.id IS NULL THEN  t1.Column1 ELSE t2.Column1 END  AS Column1,
      CASE WHEN t2.id IS NULL THEN  t1.Column2 ELSE t2.Column2 END  AS Column2
    FROM
      Table1          AS t1
    FULL OUTER JOIN
      Table2          AS t2
        ON  t1.Column1 = t2.Column2
        AND t1.Column2 = t2.Column2
    WHERE
      COALESCE(t2.Column3, t1.Column3) = 'String'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a FrameLayout view which contains one (MapView-like) control and some additional buttons
I have a sql(2005) view which is running very very slow its takes 5
I have a view which is defined as a SELECT bellow. Is there any
i have view controller which contains a button which show the image library ,if
I have a view which is composed of top, left and bottom headers and
I have a view which was working fine when I was joining my main
I have a view which I built in Interface builder with a tableview and
I have 5 view which have deferent json data , in all my 5
I have a view with two subviews: A UIImageView A small Custom View which
I have an admin view which contains four foreign keys each with a few

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.