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_idPretty gnarly looking huh? The run time of that query is 8ms. Yes, MySQL has to perform 9 subqueries and then the outer query. And because it can use good keys for the subqueries, the total execution time for this query is only 8ms. The data comes back from the database ready to use in one trip to the server. The page generation time for those pages went from a mean of 213ms with a standard deviation of 136ms to a mean of 196ms and standard deviation of 81ms. That may not sound like a lot. Take a look at how much less work the MySQL servers are doing now.

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?

MySQL是一種開源的關係型數據庫管理系統,主要用於快速、可靠地存儲和檢索數據。其工作原理包括客戶端請求、查詢解析、執行查詢和返回結果。使用示例包括創建表、插入和查詢數據,以及高級功能如JOIN操作。常見錯誤涉及SQL語法、數據類型和權限問題,優化建議包括使用索引、優化查詢和分錶分區。

MySQL是一個開源的關係型數據庫管理系統,適用於數據存儲、管理、查詢和安全。 1.它支持多種操作系統,廣泛應用於Web應用等領域。 2.通過客戶端-服務器架構和不同存儲引擎,MySQL高效處理數據。 3.基本用法包括創建數據庫和表,插入、查詢和更新數據。 4.高級用法涉及復雜查詢和存儲過程。 5.常見錯誤可通過EXPLAIN語句調試。 6.性能優化包括合理使用索引和優化查詢語句。

選擇MySQL的原因是其性能、可靠性、易用性和社區支持。 1.MySQL提供高效的數據存儲和檢索功能,支持多種數據類型和高級查詢操作。 2.採用客戶端-服務器架構和多種存儲引擎,支持事務和查詢優化。 3.易於使用,支持多種操作系統和編程語言。 4.擁有強大的社區支持,提供豐富的資源和解決方案。

InnoDB的鎖機制包括共享鎖、排他鎖、意向鎖、記錄鎖、間隙鎖和下一個鍵鎖。 1.共享鎖允許事務讀取數據而不阻止其他事務讀取。 2.排他鎖阻止其他事務讀取和修改數據。 3.意向鎖優化鎖效率。 4.記錄鎖鎖定索引記錄。 5.間隙鎖鎖定索引記錄間隙。 6.下一個鍵鎖是記錄鎖和間隙鎖的組合,確保數據一致性。

MySQL查询性能不佳的原因主要包括没有使用索引、查询优化器选择错误的执行计划、表设计不合理、数据量过大和锁竞争。1.没有索引导致查询缓慢,添加索引后可显著提升性能。2.使用EXPLAIN命令可以分析查询计划,找出优化器错误。3.重构表结构和优化JOIN条件可改善表设计问题。4.数据量大时,采用分区和分表策略。5.高并发环境下,优化事务和锁策略可减少锁竞争。

在數據庫優化中,應根據查詢需求選擇索引策略:1.當查詢涉及多個列且條件順序固定時,使用複合索引;2.當查詢涉及多個列但條件順序不固定時,使用多個單列索引。複合索引適用於優化多列查詢,單列索引則適合單列查詢。

要優化MySQL慢查詢,需使用slowquerylog和performance_schema:1.啟用slowquerylog並設置閾值,記錄慢查詢;2.利用performance_schema分析查詢執行細節,找出性能瓶頸並優化。

MySQL和SQL是開發者必備技能。 1.MySQL是開源的關係型數據庫管理系統,SQL是用於管理和操作數據庫的標準語言。 2.MySQL通過高效的數據存儲和檢索功能支持多種存儲引擎,SQL通過簡單語句完成複雜數據操作。 3.使用示例包括基本查詢和高級查詢,如按條件過濾和排序。 4.常見錯誤包括語法錯誤和性能問題,可通過檢查SQL語句和使用EXPLAIN命令優化。 5.性能優化技巧包括使用索引、避免全表掃描、優化JOIN操作和提升代碼可讀性。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具