Solved

SQL and indexes and case statements

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

636 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