bad query??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pasquale

    #1

    bad query??

    Is the problem with the query below in the SELECT?? Should I be using
    PHP to do some of things I am trying to get the SELECT to do?

    What is happening is it opens 3 or 4 of the same query/process and each
    of the process status' stay at 'Copying to tmp table', and pretty much
    bring down the server.

    Thanks!

    SELECT IF(se.regtype = 'i',CONCAT(p.ln ame,',
    ',p.fname),t.te amname),p.city, CONCAT(IF(se.re gtype='i','Indi vidual','Team') ,IF(se.subevent ID[color=blue]
    >= 9 AND se.subeventID <= 13,CONCAT(' - ',categoryname, '<BR[/color]
    />'),IF(se.subev entID = 1 OR se.subeventID = 2,'','<BR
    />')),IF(se.sube ventID = 4,CONCAT('<B>Su per
    ',st.subregtype name,'</B>'),IF(se.sube ventID = 1 OR se.subeventID = 2 OR
    se.subeventID =
    20,'',CONCAT('< B>',st.subregty pename,'</B>')))),p.displ ayname FROM
    (((((((particip ant2006_copy as p
    LEFT OUTER JOIN captain2006_cop y as c ON
    p.participantID =c.relparticipa ntID)
    LEFT OUTER JOIN team2006_copy as t ON t.relcaptainID= c.captainID)
    LEFT OUTER JOIN registration200 6_copy as r ON
    p.participantID =r.relparticipa ntID)
    LEFT OUTER JOIN regdsubevt2006_ copy as rs ON
    rs.relregistrat ionID=r.registr ationID)
    LEFT OUTER JOIN subevent2006_co py as se ON
    rs.relsubeventI D=se.subeventID )
    LEFT OUTER JOIN category2006_co py as ct ON
    se.subeventID=c t.relsubeventID )
    LEFT OUTER JOIN subregtype as st ON se.relsubregtyp eID=st.subregty peID)
    WHERE rs.relsubeventI D IN ($subevtimplode )
    ORDER BY p.lname,p.fname ,t.teamname ASC

  • bstjean

    #2
    Re: bad query??

    Can you post the table structure, the result of SHOW INDEX for the
    tables involved and the EXPLAIN for this query?

    Thank you!

    Comment

    • Pasquale

      #3
      Re: bad query??


      bstjean wrote:[color=blue]
      > Can you post the table structure, the result of SHOW INDEX for the
      > tables involved and the EXPLAIN for this query?
      >
      > Thank you![/color]


      Here's the info you wanted to see. Thanks.

      table references for EXPLAIN:

      regdsubevt2006 as rs
      subevent2006 as se
      category2006 as ct
      participant2006 as p
      captain2006 as c
      team2006 as t
      registration200 6 as r
      subregtype as st


      EXPLAIN

      table type possible_keys key key_len ref rows Extra
      rs ALL 927 Using where; Using temp
      orary;
      Using filesort
      se eq_ref PRIMARY PRIMARY 2 rs.rel
      subevent
      ID 1
      ct ALL 5
      p ALL 1244
      c ALL 23
      t ALL 15
      r ALL 907 Using where
      st eq_ref PRIMARY PRIMARY 2 se.rel
      subreg
      typeID 1





      STRUCTURE and SHOW INDEX

      all table are type=MyISAM

      participant2006
      `participantID` bigint(8) unsigned NOT NULL auto_increment,
      `fname` varchar(25) NOT NULL default '',
      `lname` varchar(25) NOT NULL default '',
      `dob` date NOT NULL default '0000-00-00',
      `age` tinyint(2) unsigned NOT NULL default '0',
      `gender` char(2) NOT NULL default '',
      `address` varchar(40) NOT NULL default '',
      `city` varchar(35) NOT NULL default '',
      `relprovstateID ` char(2) NOT NULL default '',
      `relcountryID` char(2) NOT NULL default '',
      `postalzip` varchar(11) NOT NULL default '',
      `dayphone` varchar(12) NOT NULL default '',
      `evephone` varchar(12) NOT NULL default '',
      `fax` varchar(12) NOT NULL default '',
      `email` varchar(40) NOT NULL default '',
      `shirt` char(3) NOT NULL default '',
      `displayname` char(2) NOT NULL default 'Y',
      `acknowrisk` char(2) NOT NULL default '1',
      PRIMARY KEY (`participantID `)

      Non_unique - 0
      Key_name - PRIMARY
      Seq_in_index - 1
      Column_name - participantID
      Collation - A
      Cardinality - 1244
      Sub_part
      Packed
      Null
      Index_type - BTREE

      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~
      captain2006
      `captainID` smallint(3) unsigned NOT NULL auto_increment,
      `relparticipant ID` bigint(8) unsigned NOT NULL default '0',
      PRIMARY KEY (`captainID`)

      Non_unique - 0
      Key_name - PRIMARY
      Seq_in_index - 1
      Column_name - captainID
      Collation - A
      Cardinality - 23
      Sub_part
      Packed
      Null
      Index_type - BTREE

      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~
      team2006
      `teamID` smallint(3) unsigned NOT NULL auto_increment,
      `teamname` varchar(50) NOT NULL default '',
      `relcaptainID` smallint(3) unsigned NOT NULL default '0',
      PRIMARY KEY (`teamID`)

      Non_unique - 0
      Key_name - PRIMARY
      Seq_in_index - 1
      Column_name - teamID
      Collation - A
      Cardinality - 15
      Sub_part
      Packed
      Null
      Index_type - BTREE

      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~

      registration200 6
      `registrationID ` smallint(4) unsigned NOT NULL auto_increment,
      `regdatetime` datetime NOT NULL default '0000-00-00 00:00:00',
      `relparticipant ID` bigint(8) NOT NULL default '0',
      PRIMARY KEY (`registrationI D`)

      Non_unique - 0
      Key_name - PRIMARY
      Seq_in_index - 1
      Column_name - registrationID
      Collation - A
      Cardinality - 907
      Sub_part
      Packed
      Null
      Index_type - BTREE

      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~

      regdsubevt2006
      `regdsubevtID` smallint(5) unsigned NOT NULL auto_increment,
      `relregistratio nID` smallint(4) unsigned NOT NULL default '0',
      `relsubeventID` smallint(4) unsigned NOT NULL default '0',
      PRIMARY KEY (`regdsubevtID` )

      Non_unique - 0
      Key_name - PRIMARY
      Seq_in_index - 1
      Column_name - regdsubevtID
      Collation - A
      Cardinality - 927
      Sub_part
      Packed
      Null
      Index_type - BTREE

      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~

      subevent2006
      `subeventID` smallint(4) unsigned NOT NULL auto_increment,
      `releventID` smallint(3) unsigned NOT NULL default '0',
      `regtype` char(1) NOT NULL default '',
      `relsubregtypeI D` char(2) NOT NULL default '',
      `earlybirdfee` smallint(3) unsigned NOT NULL default '0',
      `earlybirdend` date NOT NULL default '0000-00-00',
      `regularfee` smallint(3) unsigned NOT NULL default '0',
      `regularend` date NOT NULL default '0000-00-00',
      `latefee` smallint(3) unsigned NOT NULL default '0',
      `lateend` date NOT NULL default '0000-00-00',
      `dayinscost` tinyint(2) unsigned NOT NULL default '0',
      `cancelins` tinyint(2) unsigned NOT NULL default '0',
      `maxspots` smallint(4) unsigned NOT NULL default '0',
      `agemin` tinyint(2) unsigned NOT NULL default '0',
      `agemax` tinyint(2) unsigned NOT NULL default '0',
      `cutoff` date NOT NULL default '0000-00-00',
      `regopen` datetime NOT NULL default '0000-00-00 00:00:00',
      PRIMARY KEY (`subeventID`)

      Non_unique - 0
      Key_name - PRIMARY
      Seq_in_index - 1
      Column_name - subeventID
      Collation - A
      Cardinality - 22
      Sub_part
      Packed
      Null
      Index_type - BTREE

      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~

      category2006
      `categoryID` smallint(3) unsigned NOT NULL auto_increment,
      `categoryname` varchar(20) default NULL,
      `relsubeventID` smallint(4) unsigned default '0',
      PRIMARY KEY (`categoryID`)

      Non_unique - 0
      Key_name - PRIMARY
      Seq_in_index - 1
      Column_name - categoryID
      Collation - A
      Cardinality - 5
      Sub_part
      Packed
      Null
      Index_type - BTREE

      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~

      subregtype
      `subregtypeID` char(2) NOT NULL default '',
      `subregtypename ` varchar(20) NOT NULL default '',
      PRIMARY KEY (`subregtypeID` )

      Non_unique - 0
      Key_name - PRIMARY
      Seq_in_index - 1
      Column_name - subregtypeID
      Collation - A
      Cardinality - 4
      Sub_part
      Packed
      Null
      Index_type - BTREE

      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~




      Comment

      • Bill Karwin

        #4
        Re: bad query??

        By using WHERE rs.relsubeventI D IN ($subevtimplode ), you are eliminating any
        NULL rows of rs and therefore doing the same in r. This makes some of the
        OUTER JOINs irrelevant; you're effectively doing an INNER JOIN from p->r and
        r->rs. Or else you could put the condition inside the join condition for
        rs, if an OUTER JOIN is truly what you need.

        I suspect there may be other cases where you should use INNER JOIN instead
        of OUTER JOIN in this query. Can you explain why you're using OUTER JOIN in
        all these cases? The query might be doing more work than is necessary.

        The EXPLAIN output suggests that it's using a temporary file to do a
        filesort on rs. MySQL 5.0 improves filesorts greatly; you don't state which
        version of MySQL you're using.

        Do you have enough disk space for the filesort? If the disk is filling up
        during the filesort, it could cause the server to churn and finally bring
        down mysqld. Try the query again and monitor the disk space in your temp
        filesystem, to see if it tops out.

        The only indexes being used are during the joins between rs->se, and between
        se->st. Have you considered creating indexes on your "rel" fields? That
        is, the foreign key reference fields such as c.relparticipan tID,
        rs.relsubeventI D, etc. Although MyISAM tables do not implement referential
        integrity, it could benefit your join performance if you create indexes on
        these fields.

        Regarding whether you're doing too much in your select-list that you should
        perhaps do in PHP application code instead, yes, I agree. I don't think it
        is affecting your performance per se, but there's a lot of logic in the SQL
        query that should be in your application layer. You even have hardcoded
        HTML fragments, which shouldn't be in the SQL *or* the application layer --
        they should be in a presentation layer, for instance if you use an HTML
        template framework.

        Finally, are you validating that your PHP variable $subevtimplode is
        guaranteed to contain only a comma-separated list of integers? There is a
        potential for a SQL injection security vulnerability unless you validate
        this value.

        Regards,
        Bill K.


        Comment

        • Pasquale

          #5
          Re: bad query??

          Thanks a million!!

          Bill Karwin wrote:
          [color=blue]
          > By using WHERE rs.relsubeventI D IN ($subevtimplode ), you are eliminating any
          > NULL rows of rs and therefore doing the same in r. This makes some of the
          > OUTER JOINs irrelevant; you're effectively doing an INNER JOIN from p->r and
          > r->rs. Or else you could put the condition inside the join condition for
          > rs, if an OUTER JOIN is truly what you need.
          >
          > I suspect there may be other cases where you should use INNER JOIN instead
          > of OUTER JOIN in this query. Can you explain why you're using OUTER JOIN in
          > all these cases? The query might be doing more work than is necessary.[/color]

          Use of all OUTER is due to hast on my part and time constraints from the
          client wanting things done yesterday. I am definitely going to take a
          closer look at INNER and OUTER JOINs to improve the rest of my queries.

          I changed the 2 you suggested to INNER and the almost 600 results coming
          up in a flash. I decided to change others as well, and I am now only
          using OUTER JOIN for just the first 2 joins.
          [color=blue]
          >
          > The EXPLAIN output suggests that it's using a temporary file to do a
          > filesort on rs. MySQL 5.0 improves filesorts greatly; you don't state which
          > version of MySQL you're using.[/color]

          My host has version 4.0.25. They are due to upgrade soon.
          [color=blue]
          >
          > Do you have enough disk space for the filesort? If the disk is filling up
          > during the filesort, it could cause the server to churn and finally bring
          > down mysqld. Try the query again and monitor the disk space in your temp
          > filesystem, to see if it tops out.[/color]

          My host gives me 1.5GB of disk space, so with the correct JOINs used I
          would probably have plenty.
          [color=blue]
          >
          > The only indexes being used are during the joins between rs->se, and between
          > se->st. Have you considered creating indexes on your "rel" fields? That
          > is, the foreign key reference fields such as c.relparticipan tID,
          > rs.relsubeventI D, etc. Although MyISAM tables do not implement referential
          > integrity, it could benefit your join performance if you create indexes on
          > these fields.
          >
          > Regarding whether you're doing too much in your select-list that you should
          > perhaps do in PHP application code instead, yes, I agree. I don't think it
          > is affecting your performance per se, but there's a lot of logic in the SQL
          > query that should be in your application layer. You even have hardcoded
          > HTML fragments, which shouldn't be in the SQL *or* the application layer --
          > they should be in a presentation layer, for instance if you use an HTML
          > template framework.
          >
          > Finally, are you validating that your PHP variable $subevtimplode is
          > guaranteed to contain only a comma-separated list of integers? There is a
          > potential for a SQL injection security vulnerability unless you validate
          > this value.[/color]

          The value coming in is an event id which is then used to retrieve
          subevent ids that are imploded and used with IN. It is also validated.[color=blue]
          >
          > Regards,
          > Bill K.
          >
          >[/color]

          Comment

          Working...