How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Alex Your Own Question

Alex
Alex, Engineer
Category: Homework
Satisfied Customers: 2639
Experience:  BS in Business Administration with a major in MIS. 15+ years experience in software design and development.
50749495
Type Your Homework Question Here...
Alex is online now
A new question is answered every 9 seconds

I have the following SQL statement for querying users (teachers)

Resolved Question:

I have the following SQL statement for querying users (teachers) and excluding any records that have the same first name, surname and school name. The query runs really slowly and am looking to make it faster and more efficient.

SELECT ggteachers.TCHR_NO, ggteachers.REFNO, ggteachers.TCHR_TITLE, ggteachers.TCHR_FNAME, ggteachers.TCHR_SNAME, ggteachers.EMAIL, ggteachers.PASSWORD, ggteachers.regdate, ggteachers.areyoua, ggteachers.areyouaother, ggteachers.marketing, ggteachers.newsletter, ggteachers.lastlogin, ggschools.scl_name, ggschools.refno, ggschools.street, ggschools.town, ggschools.county, ggschools.postcode, ggschools.la_name, ggschools.country, ggschools.tel_std, ggschools.tel_num, ggschools.regtypeid, ggteachers.lastLogin, ggteachers.statusid, ggteachers.regtypeid, ggteachers.logincount FROM ggteachers JOIN ggschools ON (ggteachers.refno=ggschools.refno) WHERE 1 = 1 AND (ggschools.phase = 'Primary' OR ggschools.phase = 'Middle Deemed Primary' OR ggschools.phase = 'Not applicable' OR ggschools.phase = 'PRU' OR ggschools.phase = 'Special School' OR ggteachers.areyoua = 'Primary Teaching Assistant/Learning Mentor' OR ggteachers.areyoua = 'Primary Teacher' OR ggteachers.areyoua = 'Supply Teacher' OR ggteachers.areyoua = 'Student Teacher' OR ggteachers.areyoua = 'Foundation Stage Teacher') AND la_name = ? AND country = ? GROUP BY tchr_sname, tchr_fname, ggschools.scl_name ORDER BY regdate DESC LIMIT 250 OFFSET 0
Submitted: 1 year ago.
Category: Homework
Expert:  Susan Athena replied 1 year ago.
Hi. Thanks for your question.

I recommend the following changes (from easiest to hardest to implement):

1.
Change your WHERE clause from this:

WHERE 1 = 1 AND (ggschools.phase = 'Primary' OR ggschools.phase = 'Middle Deemed Primary' OR ggschools.phase = 'Not applicable' OR ggschools.phase = 'PRU' OR ggschools.phase = 'Special School' OR ggteachers.areyoua = 'Primary Teaching Assistant/Learning Mentor' OR ggteachers.areyoua = 'Primary Teacher' OR ggteachers.areyoua = 'Supply Teacher' OR ggteachers.areyoua = 'Student Teacher' OR ggteachers.areyoua = 'Foundation Stage Teacher') AND la_name = ? AND country = ?

to this:

WHERE (ggschools.phase IN ('Primary', 'Middle Deemed Primary', 'Not applicable', 'PRU', 'Special School') OR ggteachers.areyoua IN ('Primary Teaching Assistant/Learning Mentor', 'Primary Teacher', 'Supply Teacher', 'Student Teacher', 'Foundation Stage Teacher')) AND la_name = ? AND country = ?

Do you really mean OR above, or is it AND?

2. Add indexes (allowing duplicates) on ggschools.phase and ggteachers.areyoua if they don't exist.

If you don't have access to the database, the DBA should be able to do this change in a few minutes.

3.

Normalize your database by making a table called Phases that assigns a unique identifier to the phase types, and by making a table called TeacherType (aka Areyoua) that assigns a unique numeric identifier to each Teacher type. Then, in the Schools and Teachers tables, respectively, store only the ID instead of the text of each type. You may want to do the same for the Country field.

Then you can query against integers instead of text fields, which is far more efficient. Depending on your database size and complexity, this could take a fair amount of effort to implement, but is almost certain to solve the performance problems (as well as improving your database design overall).

http://databases.about.com/od/specificproducts/a/2nf.htm

Kind regards,
Susan
Customer: replied 1 year ago.

Hi


 


Thanks for getting back to me. I've had a look at what I sent and I've sent the wrong snippet of SQL. This one actually has the code to remove any records that have the same first name, surname and school name. Can you see how I would make this more efficient?


 


Cheers


 


Shaun


 


SELECT ggteachers.TCHR_NO, ggteachers.REFNO,
ggteachers.TCHR_TITLE, ggteachers.TCHR_FNAME, ggteachers.TCHR_SNAME,
ggteachers.EMAIL, ggteachers.PASSWORD, ggteachers.regdate, ggteachers.areyoua,
ggteachers.areyouaother, ggteachers.marketing, ggteachers.newsletter,
ggteachers.lastlogin, ggschools.scl_name, ggschools.refno, ggschools.street,
ggschools.town, ggschools.county, ggschools.postcode, ggschools.la_name,
ggschools.country, ggschools.tel_std, ggschools.tel_num, ggschools.regtypeid,
ggteachers.lastLogin, ggteachers.statusid, ggteachers.regtypeid,
ggteachers.logincount FROM ggteachers JOIN ggschools ON
(ggteachers.refno=ggschools.refno) JOIN ( SELECT tchr_no FROM ( SELECT DISTINCT
tchr_no, tchr_sname, tchr_fname, ggschools.scl_name, ggschools.postcode,
ggteachers.refno, MIN(DATE_FORMAT(regdate, '%Y-%m-01')) AS regdate FROM
ggteachers JOIN ggschools ON ggteachers.refno = ggschools.refno GROUP BY
tchr_sname, tchr_fname, ggschools.scl_name, ggschools.postcode ) AS teachers )
AS unique_teachers ON (unique_teachers.tchr_no=ggteachers.tchr_no) ORDER BY
regdate DESC LIMIT 250 OFFSET 0

Expert:  Susan Athena replied 1 year ago.
Hi. Thanks for your question!

I'm not sure about this one. The normalization problems don't apply here, but it's a complex query. I'd probably add indexes on refno (in the schools and teachers table), school name and postcode in the schools table, and teacher number in the teachers table, if they don't exist.

Other than that, you could try your database system's query optimizer. That will determine the optimal query plan for ordering the uniques and joins.

If the query is really unacceptably slow, you could also try a staging table. That is: first run a make-table query to set up a temporary table with the unique teacher information (grouped), and then run a secondary select query against that. You'd want to set it up as a stored procedure so that the two processes would always run together. That's not the most elegant solution, but it can certainly improve performance.

Kind regards,
Susan

Customer: replied 1 year ago.
Relist: Answer quality.
I'm looking for someone to help rewrite the sql query to be more efficient. The help offered was information that I already know.
Expert:  Alex replied 1 year ago.
What DBS are you using?

Are you wanting to remove the duplicates or only show the duplicated values once?

Can you post the DDL to create your tables?

How many records are in those tables, need to determine if it an index issue or simply a problem with your query.

Thanks
Alex

Customer: replied 1 year ago.


Hi Alex


 


Thanks for getting in contact. It's MySQL 5.6. I want to remove all the duplicates from the query.


 


43,000 records


 


SET FOREIGN_KEY_CHECKS=0;


DROP TABLE IF EXISTS `schools`; CREATE TABLE `schools` ( `REFNO` int(11) NOT NULL AUTO_INCREMENT, `LA_NAME` varchar(255) DEFAULT NULL, `LA` int(11) DEFAULT NULL, `LA_NO` int(11) DEFAULT NULL, `areaid` int(11) DEFAULT '0', `DFES_NO` int(11) DEFAULT NULL, `SCL_NAME` varchar(255) DEFAULT NULL, `STREET` varchar(255) DEFAULT NULL, `LOCALITY` varchar(255) DEFAULT NULL, `ADDRESS_3` varchar(255) DEFAULT NULL, `TOWN` varchar(255) DEFAULT NULL, `COUNTY` varchar(255) DEFAULT NULL, `POSTCODE` varchar(12) DEFAULT NULL, `TEL_STD` varchar(11) DEFAULT NULL, `TEL_NUM` int(11) DEFAULT NULL, `PHASE` varchar(255) DEFAULT NULL, `COUNTRY` varchar(255) DEFAULT NULL, `countryid` int(11) DEFAULT '0', `regtypeid` int(1) DEFAULT '-1', `datecreated` datetime DEFAULT NULL, `datemodified` datetime DEFAULT NULL, PRIMARY KEY (`REFNO`), KEY `LA_NO` (`LA_NO`) USING BTREE, KEY `refno` (`REFNO`) USING BTREE, KEY `regtypeid` (`regtypeid`) USING BTREE, KEY `phase` (`PHASE`) ) ENGINE=InnoDB AUTO_INCREMENT=10024211 DEFAULT CHARSET=latin1 COMMENT='InnoDB free: 23552 kB; InnoDB free: 13312 kB; InnoDB free: 2';


 


42,000 records


 


SET FOREIGN_KEY_CHECKS=0;


DROP TABLE IF EXISTS `teachers`; CREATE TABLE `teachers` ( `TCHR_NO` varchar(35) NOT NULL DEFAULT '0', `REFNO` int(11) DEFAULT NULL, `TCHR_TITLE` varchar(10) DEFAULT NULL, `TCHR_FNAME` varchar(20) DEFAULT NULL, `TCHR_SNAME` varchar(30) DEFAULT NULL, `EMAIL` varchar(100) DEFAULT NULL, `PASSWORD` varchar(255) DEFAULT NULL, `regdate` date DEFAULT NULL, `datemodified` datetime DEFAULT NULL, `areyoua` varchar(255) DEFAULT NULL, `areyouaother` varchar(255) DEFAULT NULL, `newsletter` int(1) NOT NULL DEFAULT '1', `accounttype` varchar(255) DEFAULT NULL, `marketing` int(1) NOT NULL DEFAULT '1', `regtypeid` int(1) DEFAULT NULL, `requpdate` int(11) DEFAULT '0', `logincount` int(11) DEFAULT '0', `lastlogin` datetime DEFAULT '0000-00-00 00:00:00', `statusid` int(11) NOT NULL DEFAULT '5', PRIMARY KEY (`TCHR_NO`), KEY `tchr_no` (`TCHR_NO`) USING BTREE, KEY `refno` (`REFNO`) USING BTREE, KEY `regtypeid` (`regtypeid`) USING BTREE, KEY `statusid` (`statusid`) USING BTREE, KEY `areyoua` (`areyoua`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='InnoDB free: 11264 kB; InnoDB free: 16384 kB; InnoDB free: 2';


 


Let me know if you need anything else.


 


Cheers


 


Shaun

Expert:  Alex replied 1 year ago.
Ok, I will look at this tonight.
Expert:  Alex replied 1 year ago.
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
If you've already paid for this answer, simply Login.
Alex, Engineer
Category: Homework
Satisfied Customers: 2639
Experience: BS in Business Administration with a major in MIS. 15+ years experience in software design and development.
Alex and 10 other Homework Specialists are ready to help you

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
< Last | Next >
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
  • He answered my question promptly and gave me accurate, detailed information. If all of your experts are half as good, you have a great thing going here. Diane Dallas, TX
 
 
 

Meet The Experts:

 
 
 
  • Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4522
    More than 5000 online tutoring sessions.
< Last | Next >
  • http://ww2.justanswer.com/uploads/BU/BusinessTutor/2012-2-2_115741_Kouki2.64x64.jpg Manal Elkhoshkhany's Avatar

    Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4522
    More than 5000 online tutoring sessions.
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Engineer

    Satisfied Customers:

    3458
    Expert in Java C++ C C# VB Javascript Design SQL HTML
  • http://ww2.justanswer.com/uploads/LI/lindaus/2012-6-10_04811_IMG20120609164157.64x64.jpg Linda_us's Avatar

    Linda_us

    Finance, Accounts & Homework Tutor

    Satisfied Customers:

    3124
    Post Graduate Diploma in Management (MBA)
  • http://ww2.justanswer.com/uploads/chooser77/2009-08-18_162025_Chris.jpg Chris M.'s Avatar

    Chris M.

    M.S.W. Social Work

    Satisfied Customers:

    2385
    Master's Degree, strong math and writing skills, experience in one-on-one tutoring (college English)
  • http://ww2.justanswer.com/uploads/JawaadAhmed/2009-6-27_12137_SIs_SHadi.jpg F. Naz's Avatar

    F. Naz

    Chartered Accountant

    Satisfied Customers:

    1988
    Experience with chartered accountancy
  • http://ww2.justanswer.com/uploads/JK/jkcpa/2011-1-16_182614_jkcpa.64x64.jpg Bizhelp's Avatar

    Bizhelp

    CPA

    Satisfied Customers:

    1876
    Bachelors Degree and CPA with Accounting work experience
  • http://ww2.justanswer.com/uploads/avremote/photoa.jpg Seanna's Avatar

    Seanna

    Tutor

    Satisfied Customers:

    1781
    3,000+ satisfied customers, all topics, A+ work
 
 
 

Related Homework Questions

Chat Now With A Tutor
Alex
Alex
Tutor
2639 Satisfied Customers
BS in Business Administration with a major in MIS. 15+ years experience in software design and development.