Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL and indexes and case statements

Posted on 2011-09-20
3
Medium Priority
?
176 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 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

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.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

886 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