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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:04:31+00:00 2026-05-24T19:04:31+00:00

I have the following two tables: CREATE TABLE Test_Main ( [ID] INT IDENTITY ,

  • 0

I have the following two tables:

CREATE TABLE Test_Main
    (
      [ID] INT IDENTITY ,
      [TypeID] INT ,
      [BookID] INT
    )

CREATE TABLE Test_Second
    (
      [TypeID] INT ,
      [BookID] INT ,
      [Value] INT,

    )

INSERT  INTO Test_Main(TypeID, BookID)
   SELECT 1, 10
   UNION
   SELECT 2, 31
   UNION
   SELECT 3, 51
   UNION
   SELECT 4, 81

INSERT INTO Test_Second(TypeID, BookID, Value)
   SELECT  1, 0, 30
   UNION
   SELECT  2, 31, 45
   UNION
   SELECT  3, 51, 66
   UNION
   SELECT  4, 0, 22 

and I have the following query :

SELECT  
    ID ,
    Test_Main.TypeID ,
    Test_Main.BookID
FROM
    Test_Main
INNER JOIN 
    Test_Second ON Test_Main.[TypeID] = Test_Second.[TypeID]
WHERE   
    Test_Main.BookID = CASE WHEN (Test_Main.BookID = 2 OR Test_Main.BookID = 3) 
                                THEN Test_Second.BookID
                            ELSE Test_Main.BookID
                       END 

This query is giving the following output:

ID          TypeID      BookID
----------- ----------- -----------
1           1           10
2           2           31
3           3           51
4           4           81

Now when my table has million rows I suspect my case clause will cause the performance issue. How can I optimize the query to use index seek. What should be the index for the above 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-05-24T19:04:33+00:00Added an answer on May 24, 2026 at 7:04 pm

    You need to have the appropriate indices in place:

    • The column TypeID should be indexed on both tables (since it’s used in the JOIN condition)
    • The column Test_Main.BookID needs to be indexed since it’s used in the WHERE clause

    With those three indices in place, I believe your query should work quite nicely, even with lots of rows in the tables!

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

Sidebar

Related Questions

For example I have the following tables resulting from: CREATE TABLE A (Id int,
I have the following two tables (simplified for this question): CREATE TABLE team (
I have the following two tables CREATE TABLE accounts ( id INTEGER UNSIGNED NOT
I have the following two tables, affiliates and referrers. affiliates Table id loginid 3
I have the following two tables: Customer { int Id int Name } Bills
I have the following two mySQL tables with the 'child' table having a DELETE
I have two tables, categories and products. I am using CodeIgniter. Categories CREATE TABLE
I have the following table: CREATE TABLE `events` ( `evt_id` int(11) NOT NULL AUTO_INCREMENT,
I have create two tables with reference with another table: I like this: Table1:
I have the following two tables: Table1 ---------- ID Name 1 A 2 B

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.