Solved

SQL and indexes and case statements

Posted on 2011-09-20
3
156 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
3 Comments
 
LVL 37

Accepted Solution

by:
Neil Russell earned 167 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 166 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 167 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now