3

When I CALL this proc in MySQL, it just shows LOADING... and even freezes my server:

delimiter $$
CREATE PROCEDURE MyProc(IN index1 INT, IN count1 INT, IN index2 INT, IN count2 INT)
BEGIN
    SELECT id FROM view1 WHERE column1 IS NULL OR column1 = 1 ORDER BY dateColumn DESC LIMIT index1, count1;
    SELECT id FROM view1 WHERE column2 = 0 ORDER BY column3 DESC LIMIT index2, count2;
END$$
delimiter ;

If I only have one of either SELECTs, it works.

Anyone have any idea what I'm missing/doing wrong?

Many thanks in advance!

Details

I'm creating a proc for PHP so that I don't have to make so many connections to the database. Each SELECT needs to be its' own result set with column information for ease in PHP.

I need the slightly extra speed because my site is heavily dependent upon dynamic pagination.

  • 1
    Not sure why it would hang, but you probably want a union between the two selects to get a single result set – PeterJ Dec 29 '12 at 05:07
  • you can also try Set @someVar := (select .....) – Suresh Kamrushi Dec 29 '12 at 05:09
  • @PeterJ Thanks for looking! Yeah, I pretty much ripped it out of the docs, so I have no idea what gives. As for `UNION`, the n+1 result sets don't have column names, right? –  Dec 29 '12 at 05:09
  • @SureshKamrushi I appreciate your time! Do I return `@someVar`? MySQL obviously isn't my strong suit. ;P –  Dec 29 '12 at 05:10
  • Not sure how that works in MySQL, I use SQL/Server more which from memory keeps the column names OK (if they are the same) – PeterJ Dec 29 '12 at 05:10
  • @PeterJ Yeah, thought that was the case. This sucker will end up having like 6 individual `SELECT`s by the time I'm done, and I need them to keep their column names for ease in PHP, so this is the best I can come up with now. –  Dec 29 '12 at 05:13
  • @JoeCoderGuy Try to run this two query seperately and find how much time have been taken by both queries? – Saharsh Shah Dec 29 '12 at 05:24
  • @SaharshShah They both come back "instantly", by themselves or in the proc. It's just when they're together, something goes wrong. I'm doing it this crazy way because I need the 10s of ms that's wasted in constantly opening and closing database connections. –  Dec 29 '12 at 05:26

2 Answers2

1

What you are doing is quite sensible. This looks like a defect in phpmyadmin that may have been recently fixed, where stored procedures returning multiple result sets were not handled properly.

http://sourceforge.net/tracker/?func=detail&atid=377408&aid=3474853&group_id=23067

Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427
  • Thank-you Michael. I have 3.5.1. Do I need to upgrade? –  Dec 29 '12 at 06:11
  • Just upgraded to latest. Still hanging. Jeez, this is ridiculous. Thanks for your help! (and the upgrade) –  Dec 29 '12 at 06:16
  • For the record, the first selection (Adminer) here http://www.webdesigneronline.co.uk/10-great-alternatives-to-phpmyadmin worked. –  Dec 29 '12 at 06:28
  • 1
    There have been a few releases since 3.5.1 but it's hard to tell from the release notes whether that fix has been pushed into any of them. I always test multi-result-set SPs with the mysql command line client, because it handles them flawlessly, so that would be my recommendation for a guaranteed solution. – Michael - sqlbot Dec 29 '12 at 06:29
  • 1
    The fix for this bug is in the master branch of phpMyAdmin at the moment. Which means that it will be available in version 4.0 – Roccivic Dec 31 '12 at 14:24
0

Try this:

DELIMITER $$
CREATE PROCEDURE MyProc(IN index1 INT, IN count1 INT, IN index2 INT, IN count2 INT)
BEGIN
    SET @s = CONCAT('SELECT id FROM view1 WHERE column1 IS NULL OR column1 = 1 ORDER BY dateColumn DESC LIMIT ', index1, ', ', count1);
    SET @s1 = CONCAT('SELECT id FROM view1 WHERE column2 = 0 ORDER BY column3 DESC LIMIT ', index2, ', ', count2);
    PREPARE stmt FROM @s;
    PREPARE stmt1 FROM @s1;
    EXECUTE stmt;
    EXECUTE stmt1;
    DEALLOCATE PREPARE stmt;
    DEALLOCATE PREPARE stmt1;
END$$
DELIMITER ;
Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
  • Thank-you very much for trying! For some reason, it gives `#1064` at `@index1` for the first proc. The second proc takes, but it gives the same result as my original. Ugh! Is there some setting that I have to turn on to allow multiple result sets in phpMyAdmin? –  Dec 29 '12 at 05:45
  • Oh! I misread! `#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@index1, @count1; SELECT id FROM view1' at line 12` Is `LIMIT` with index AND count forbidden in MySQL stored proc? –  Dec 29 '12 at 05:50
  • I am removing my first query and want to know what is the issue in second query – Saharsh Shah Dec 29 '12 at 05:57
  • I think this might be it: http://dbaspot.com/mysql/378711-stored-procedure-problem-variable-limit.html –  Dec 29 '12 at 05:59
  • The proc you have left gives no error, but when I `CALL`, it hangs just as my original. Need to find a another program to test with... –  Dec 29 '12 at 06:21