Keeping your data work on the server using UNION_MySQL
I have found myself using UNION in MySQL more and more lately. In this example, I am using it to speed up queries that are using IN clauses. MySQL handles the IN clause like a big OR operation. Recently, I created what looks like a very crazy query using UNION, that in fact helped our MySQL servers perform much better.
With any technology you use, you have to ask yourself, "What is this tech good at doing?" For me, MySQL has always been excelent at running lots of small queries that use primary, unique, or well defined covering indexes. I guess most databases are good at that. Perhaps that is the bare minimum for any database. MySQL seems to excel at doing this however. We had a query that looked like this:
select category_id, count(*) from some_table<br>where<br>article_id in (1,2,3,4,5,6,7,8,9) and<br>category_id in (11,22,33,44,55,66,77,88,99) and<br>some_date_time > now() - interval 30 day<br>group by<br>category_id
There were more things in the where clause. I am not including them all in these examples. MySQL does not have a lot it can do with that query. Maybe there is a key on the date field it can use. And if the date field limits the possible rows, a scan of those rows will be quick. That was not the case here. We were asking for a lot of data to be scanned. Depending on how many items were in the in clauses, this query could take as much as 800 milliseconds to return. Our goal at DealNews is to have all pages generate in under 300 milliseconds. So, this one query was 2.5x our total page time.
In case you were wondering what this query is used for, it is used to calculate the counts of items in sub categories on our category navigation pages. On this page it's the box on the left hand side labeled "Category". Those numbers next to each category are what we are asking this query to return to us.
Because I know how my data is stored and structured, I can fix this slow query. I happen to know that there are many fewer rows for each item for article_id than there is for category_id. There is also a key on this table on article_id and some_date_time. That means, for a single article_id, MySQL could find the rows it wants very quickly. Without using a union, the only solution would be to query all this data in a loop in code and get all the results back and reassemble them in code. That is a lot of wasted round trip work for the application however. You see this pattern a fair amount in PHP code. It is one of my pet peeves. I have written before about keeping the data on the server . The same idea applies here. I turned the above query into this:
select category_id, sum(count) as count from <br>(<br> (<br> select category_id, count(*) as count from some_table<br> where<br> article_id=1 and<br> category_id in (11,22,33,44,55,66,77,88,99) and<br> some_date_time > now() - interval 30 day<br> group by<br> category_id<br> )<br> union all<br> (<br> select category_id, count(*) as count from some_table<br> where<br> article_id=2 and<br> category_id in (11,22,33,44,55,66,77,88,99) and<br> some_date_time > now() - interval 30 day<br> group by<br> category_id<br> )<br> union all<br> (<br> select category_id, count(*) as count from some_table<br> where<br> article_id=3 and<br> category_id in (11,22,33,44,55,66,77,88,99) and<br> some_date_time > now() - interval 30 day<br> group by<br> category_id<br> )<br> union all<br> (<br> select category_id, count(*) as count from some_table<br> where<br> article_id=4 and<br> category_id in (11,22,33,44,55,66,77,88,99) and<br> some_date_time > now() - interval 30 day<br> group by<br> category_id<br> )<br> union all<br> (<br> select category_id, count(*) as count from some_table<br> where<br> article_id=5 and<br> category_id in (11,22,33,44,55,66,77,88,99) and<br> some_date_time > now() - interval 30 day<br> group by<br> category_id<br> )<br> union all<br> (<br> select category_id, count(*) as count from some_table<br> where<br> article_id=6 and<br> category_id in (11,22,33,44,55,66,77,88,99) and<br> some_date_time > now() - interval 30 day<br> group by<br> category_id<br> )<br> union all<br> (<br> select category_id, count(*) as count from some_table<br> where<br> article_id=7 and<br> category_id in (11,22,33,44,55,66,77,88,99) and<br> some_date_time > now() - interval 30 day<br> group by<br> category_id<br> )<br> union all<br> (<br> select category_id, count(*) as count from some_table<br> where<br> article_id=8 and<br> category_id in (11,22,33,44,55,66,77,88,99) and<br> some_date_time > now() - interval 30 day<br> group by<br> category_id<br> )<br> union all<br> (<br> select category_id, count(*) as count from some_table<br> where<br> article_id=9 and<br> category_id in (11,22,33,44,55,66,77,88,99) and<br> some_date_time > now() - interval 30 day<br> group by<br> category_id<br> )<br>) derived_table<br>group by<br> category_id

The arrow in the image is when I rolled the change out. Several other graphs show the change in server performance as well.
The UNION is a great way to keep your data on the server until it's ready to come back to your application. Do you think it can be of use to you in your application?

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了使用MySQL的Alter Table語句修改表,包括添加/刪除列,重命名表/列以及更改列數據類型。

文章討論了為MySQL配置SSL/TLS加密,包括證書生成和驗證。主要問題是使用自簽名證書的安全含義。[角色計數:159]

文章討論了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比較了它們對初學者和高級用戶的功能和適合性。[159個字符]

本文討論了使用Drop Table語句在MySQL中放下表,並強調了預防措施和風險。它強調,沒有備份,該動作是不可逆轉的,詳細介紹了恢復方法和潛在的生產環境危害。

InnoDB的全文搜索功能非常强大,能够显著提高数据库查询效率和处理大量文本数据的能力。1)InnoDB通过倒排索引实现全文搜索,支持基本和高级搜索查询。2)使用MATCH和AGAINST关键字进行搜索,支持布尔模式和短语搜索。3)优化方法包括使用分词技术、定期重建索引和调整缓存大小,以提升性能和准确性。

本文討論了在PostgreSQL,MySQL和MongoDB等各個數據庫中的JSON列上創建索引,以增強查詢性能。它解釋了索引特定的JSON路徑的語法和好處,並列出了支持的數據庫系統。
