For a long time I tolerated writing or not writing the answer, but I could not overcome myself, in the end I would still write.
From what the previous speakers have written, I can only summarize.
The question is what is a complex query to the database?
Here you can discuss for a long time. On the one hand, a complex query to a database is a query which, in essence, must return only one value or one column from a table. Imagine that we have a table in which there are 1 million records , say, 15 fields of different types , and Blob, in addition, where they are stored, let's say pictures up to 5Mb . We need to get the name of the pictures, 300 pictures starting from 900 000 and ending with 900 300 they are in the name_picture field but the query looks like this
SELECT * FROM af_products WHERE d_upload>'2010-11-02' and d_upload<'2011-12-22' ORDER BY `id` LIMIT 900000,300
All fields will be selected here. Is everyone with math friends? Let's say the average size of a photo is 3.5 MB multiplied by 300 selected records 1 GB. just chosen from the database, the execution time of the script may be small, but the number of selected fields is wrong. But most likely this query does not relate to the complexity of the query, but to the stupidity of the developer, who doesn’t care about optimization, even as simple as selecting only the necessary fields from the tables.
A complex query to the database is such a query. We touch several tables + we make sampling by means of LIKE. Probably difficult, although it is performed fairly quickly, with 10K entries in the table. This is the code from the module for ModX, wrote the dictionary.
SELECT dic.name as dic_name, dic.id as dic_id,dw.id,dw.alias,dw.short_desc, dw.name AS d_name FROM `dictionary_word` dw LEFT JOIN `word` ww ON ww.id=dw.word_id LEFT JOIN `dictionary` dic ON dw.dictionary_id=dic.id where ww.name like '%".$_GET['search']."%' and dic.id=".$_GET['sid']." and dic.public=0
Still, from a nationwide and generally accepted complex query to a database, this is a query that affects several tables, i.e. JOIN happens. But in practice, the complexity of a query is shaped by many factors; if you look at the first query, then the developer’s nonsense is there, the second one is large, but it is executed quickly. All the same, a complex query is one that takes definitely more time to execute that you were counting on, and implies optimization if it is possible. And it is possible in several ways, either by changing the conditions or breaking it up into a few more simple ones. You can experiment a lot, here the main thing is the correct final effect. also if you look at any request with
LIMIT 900000,300
We understand that at first all 900300 records will be selected and only then the 300 we need will be sorted - this is also a complicated query to the database, although the human factor to not do so is present.
можно ли одним запросом обратиться к нескольким БД?
In it is possible, and especially it is possible if they say so in one DB and are broken by prefixes. In fact, several databases are obtained, i.e. There can be many sites in one database, tables are the same only prefixes differ, for business card sites this is an option. And as a matter of fact, we cannot connect to one database. those.
$link1=mysql_connect(); $link2=mysql_connect();
2 different connections, and the request intended for BD 1 will not be executed for BD 2. So I think it’s hard to choose the right answer, they are essentially all right, or at least they are looking in the right direction.
I have everything as usual :)
ps 3341 letters / 538 words