php - MySQL Union query taking too much time to execute -
I am trying to get a single MySQL query result with different questions working on a separate table and database, My table structure is with database names:
database database and this queue Minister whom I am trying to execute I'm sure this is not the best way to do this, just because I have to wait approximately 5 minutes to get the results. Is there any way to do this very fast? I have already set an index on all 3 db This is a clear result: a) You should not create such cross-database queries here. With its DB structure, it will be better suited to run 3 independent questions and to combine results using a few simple scripts or to create a temporary table in a database, where you can add data from each one of them b) Put a large database to improve some indexed speed. This is not a solution though ... You can try playing with the temporary tables: ads1 - tables:
clicks_214 ,
request < P> database
Ads2 - tables:
click_255 ,
request
ads3 - tables:
clicks_256 ,
request
$ query = ""; ($ I = 1; $ i & lt; 4; ++ $ i) for {$ this-> Db_num = $ i; $ This- & gt; SelectDB ($ i); $ Table = $ this- & gt; GetTableName (); $ Db = $ this- & gt; GetDatabaseName (); $ Query = "(SELECT r.kwd, count (c.rid) as cnum, from rnum as count (r.kwd) $ db.requests r LEFT JOIN $ db. $ Table c ON r.id = c .rid where limit = $ $ hidden from the HID = '$ hidden' and '. $ Date1 $ hour1: 00: 00' and '$ date2 $ hour2: 59: 59' by the RKWD order, CNM DESC limit, 50) "; If ($ i <3) {$ query. = "UNION"; }}
$ this-> Selection DB ('temptableDB'); $ Maketemp = "create temporary table temp_table_1 (` kwd` int, `rid` int,)"; Mysql_query ($ maketemp, $ connection) or die ("SQL error:" .mysql_error ()); ($ I = 1; $ i & lt; 4; ++ $ i) for {$ this-> Db_num = $ i; $ This- & gt; SelectDB ($ i); $ Table = $ this- & gt; GetTableName (); $ Db = $ this- & gt; GetDatabaseName (); $ Inserttemp = "Include in temptableDB.temp_table_1 (` kwd`, `redDay ') SELECT r.kwd, add rig from c.rid $ db.requests $ db. $ Table c ON r.id = c.rid WHERE Hidden = '$ hidden' and '. $ Date1 $ hour1: 00: 00' and '$ date2 $ hour2: 59: 59' between R. time; Mysql_query ($ inserttemp, $ connection) or die ("SQL error:" .mysql_error ()); } $ Select = "SELECT kwd, cnum DESC by c.c.d. rw.kwd ORDER by counting (redemption), cnum as rnum fROM temp_table_1 group; $ Export = mysql_query ($ select, $ connection) or die ("Sql error:" .mysql_error ());
Comments
Post a Comment