Solved

SQL and indexes and case statements

Posted on 2011-09-20
3
161 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

786 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