How to make MySql View Filter each table upon receipt

I have a view that works like this:

CREATE VIEW v_myView as SELECT * FROM(
      (SELECT a,b,c,d FROM table1)
    UNION ALL
      (SELECT a,b,c,d FROM table2)
    UNION ALL
      (SELECT a,b,c,d FROM table3)
    .
    .
    .) 

When I use a view, I filter it like this:

SELECT * FROM v_myView WHERE a=x

While this works, it takes an hour (present hour, not figuratively). if I make such a request:

SELECT * FROM(
      (SELECT a,b,c,d FROM table1 WHERE a=x)
    UNION ALL
      (SELECT a,b,c,d FROM table2 WHERE a=x)
    UNION ALL
      (SELECT a,b,c,d FROM table3 WHERE a=x)
    .
    .
    .) 

it takes a minute. which made me wonder if there is a way to get MySql to do this automatically, which means the command WHEREwill work before each table is selected.

+4
source share
1 answer

As you may already know, the second method is faster because, unlike the first, it does not display all the results from all three tables and then filters it.

(fooobar.com/questions/79067/...) , , .

:

( Int )

create function p1() returns INTEGER DETERMINISTIC NO SQL return @p1;

CREATE VIEW v_myView as SELECT * FROM(
      (SELECT a,b,c,d FROM table1 WHERE a=p1() )
    UNION ALL
      (SELECT a,b,c,d FROM table2 WHERE a=p1() )
    UNION ALL
      (SELECT a,b,c,d FROM table3 WHERE a=p1() )
    .
    .
    .) 

( 12 )

SELECT * FROM (select @p1:=12 p) parm, v_myView;
+1

All Articles