Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL and indexes and case statements

Posted on 2011-09-20
3
Medium Priority
?
173 Views
Last Modified: 2012-05-12
My code is taking forever to build therefore I added indexes, and the code is still processing. Is something wrong with my indexes or case statement? I ran the indexes first and they ran. Now when I run the code, it is still processing.
alter table tmp 1  add index a1 (school_id);
alter table tmp2 add index a1 (geo_code);

DROP table if EXISTS m_v3; 
Create table m_v3 AS
select 
       a.school_id,
       a.name,
       a.adreess
       b.geo_code,
       b.sco_code,
CASE 
    when sco_code is not null then 'Y' else 'N' end as check_code
FROM tmp1  a left join tmp2  b
ON a.school_id = b.geo_code
GROUP BY 1,2;

Open in new window

0
Comment
Question by:DAN2011
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 37

Accepted Solution

by:
Neil Russell earned 668 total points
ID: 36569930
Firstly, why the GROUP BY?

Secondly i would drop the CASE statement and replace it with
'N' as check_code

Then AFTER you have run that do an UPDATE query to set check_code = 'Y' where sco_code is not null

So....

alter table tmp1  add index a1 (school_id);
alter table tmp2 add index a2 (geo_code);
 
DROP table if EXISTS m_v3;  
Create table m_v3 AS
select  
       a.school_id,
       a.name,
       a.adreess
       b.geo_code,
       b.sco_code,
       'N' as check_code
FROM tmp1  a left join tmp2  b
ON a.school_id = b.geo_code
GROUP BY 1,2;

UPDATE m_v3
SET check_code = 'Y'
WHERE sco_code is null;

(I have no server access right now but that looks right)
0
 
LVL 24

Assisted Solution

by:johanntagle
johanntagle earned 664 total points
ID: 36571688
I see nothing wrong with using the CASE statement, but like Neilsr I don't see why you are using GROUP BY - maybe you mean ORDER BY?  But I would drop that too since I believe there's no guarantee the rows will be sorted as they are stored on disk.  Just use ORDER BY when you select from the table.

How big are tables tmp1 and tmp2?  What's the output when you run the following:

EXPLAIN select
       a.school_id,
       a.name,
       a.adreess
       b.geo_code,
       b.sco_code,
CASE
    when sco_code is not null then 'Y' else 'N' end as check_code
FROM tmp1  a left join tmp2  b
ON a.school_id = b.geo_code

(Note that I already removed the GROUP BY since I don't think it's needed at all).
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 668 total points
ID: 36575360
Unfortunately the code you posted is not valid:
1. You are missing a comma after a.adreess
2. You cannot use the ordinal position of the column with a GROUP BY clause (only the ORDER BY).
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question