Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql stored procedure for search from identical tables

Hi developers I am researching ways of fast selecting rows from many tables with identical structure. Tables have too much data to be 1 table. So every table contains data for 1 month. For example log_2011_01 ,log_2011_02 etc. Tables have datetime column created_at.

I need to create stored procedure , it must contain 2 datetime parameters for searching between this dates and few others.

Now I see next realization: stored procedure must calculate collection of tables between 2 dates and generate sql to select data with union_all . I think I will be to heavy and not ready for highload,isn't it? Is there are any other possibilities to do this? Thanks

like image 374
Fivell Avatar asked Jul 26 '11 11:07

Fivell


People also ask

How do I identify all stored procedures referring a particular table in MySQL?

You need to query Mysql. proc table, here's the documentation: The mysql. proc table contains information about stored procedures and stored functions.

Can you have multiple SELECT statements in a stored procedure?

Each procedure has one or more statements. In our case, these are SQL statements. So, you can write a procedure that will – insert new data, update or delete existing, retrieve data using the SELECT statement. And even better, you can combine more (different statements) in the stored procedures.

Can stored procedure have SELECT statements?

We can not directly use stored procedures in a SELECT statement.


Video Answer


1 Answers

Agree with other comments, but I tried to help you with SP. This SP builds the query that can be executed with prepared statements.

-- SP definition:

DELIMITER $$

CREATE PROCEDURE iterateMonths(IN fromDate DATE, IN toDate   DATE)
BEGIN
  DECLARE tempDate DATE;
  DECLARE query    VARCHAR(1000); -- Query string, length might be increased

  SET tempDate = fromDate - INTERVAL (dayofmonth(fromDate) - 1) DAY;

  WHILE tempDate <= toDate
  DO

    IF query IS NOT NULL THEN
      SET query = concat(query, '\r\nUNION ALL\r\n');
    ELSE
      SET query = '';
    END IF;

    SET query = concat(query, 'SELECT * FROM log_', DATE_FORMAT(tempDate, '%Y_%m'));
    SET tempDate = tempDate + INTERVAL 1 MONTH;
  END WHILE;

  SELECT query; -- Output generated query
END
$$

DELIMITER ;

-- Call the SP

SET @fromDate = '2010-07-29';
SET @toDate = '2011-08-29';
CALL iterateMonths(@fromDate, @toDate);

-- Output:

SELECT * FROM log_2010_07
UNION ALL
SELECT * FROM log_2010_08
UNION ALL
SELECT * FROM log_2010_09
UNION ALL
SELECT * FROM log_2010_10
UNION ALL
SELECT * FROM log_2010_11
UNION ALL
SELECT * FROM log_2010_12
UNION ALL
SELECT * FROM log_2011_01
UNION ALL
SELECT * FROM log_2011_02
UNION ALL
SELECT * FROM log_2011_03
UNION ALL
SELECT * FROM log_2011_04
UNION ALL
SELECT * FROM log_2011_05
UNION ALL
SELECT * FROM log_2011_06
UNION ALL
SELECT * FROM log_2011_07
UNION ALL
SELECT * FROM log_2011_08
like image 53
Devart Avatar answered Nov 08 '22 11:11

Devart