If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > how to create index when running 'create table as select'

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-10-08, 23:34
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
how to create index when running 'create table as select'

Hello ALL,

my program has a SQL statement like
Code:
Create temporary table Temp
 AS SELECT
      Name
 FROM
     EmployeeList
 WHERE 
     Dept = 'XXX'
I would l ike to create an index on the field Name of the temporary table Temp. I wonder if I can create the index on the fly by adding sth in the above statement.

Thanks
Reply With Quote
  #2 (permalink)  
Old 12-10-08, 23:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
what the... ???

why are you creating a temporary table here?

what could you possibly want to do with it, that you cannot do with a subquery on the EmployeeList table?

methinks thou art over-engineering...

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-11-08, 00:04
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
Actually, I need to use the Name field of the temporary table in the following WHERE clause of another SQL statement.
Code:
SELECT
       *
FROM
     a, Temp
WHERE
    a.Name = Temp.Name
I think it would be more efficient if an index is created on Temp.Name.

Additionally, the actual SQL statement to create the temporary table Temp is so complicated (beside the EmployeeList table, 4 other tables are involved)that it cannot be integrated with the above statement into one single SQL statement.

Last edited by cy163; 12-11-08 at 00:11.
Reply With Quote
  #4 (permalink)  
Old 12-11-08, 00:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
dude, compare what you have with this --
Code:
SELECT a.*
  FROM a
INNER 
  JOIN ( SELECT Name
           FROM EmployeeList
          WHERE Dept = 'XXX'
       ) AS Temp
    ON Temp.Name = a.Name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 12-11-08, 07:26
cy163 cy163 is offline
Registered User
 
Join Date: Apr 2007
Posts: 127
sorry, what do you mean by showing my previous statement here.

I just wonder if it is possible to create an index in 'create temporary table as select ...' statement.
Reply With Quote
  #6 (permalink)  
Old 12-11-08, 08:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by cy163
I just wonder if it is possible to create an index in 'create temporary table as select ...' statement.
i'm just wondering why you need a temporary table at all
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 12-11-08, 08:57
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
You don't need a temporary table, what you need is a self join on your table, exactly what you have been shown in this thread.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On