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 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









この記事では、MySQLのAlter Tableステートメントを使用して、列の追加/ドロップ、テーブル/列の名前の変更、列データ型の変更など、テーブルを変更することについて説明します。

記事では、証明書の生成と検証を含むMySQL用のSSL/TLS暗号化の構成について説明します。主な問題は、セルフ署名証明書のセキュリティへの影響を使用することです。[文字カウント:159]

記事では、MySQLで大規模なデータセットを処理するための戦略について説明します。これには、パーティション化、シャード、インデックス作成、クエリ最適化などがあります。

記事では、MySQLワークベンチやPHPMyAdminなどの人気のあるMySQL GUIツールについて説明し、初心者と上級ユーザーの機能と適合性を比較します。[159文字]

この記事では、ドロップテーブルステートメントを使用してMySQLのドロップテーブルについて説明し、予防策とリスクを強調しています。これは、バックアップなしでアクションが不可逆的であることを強調し、回復方法と潜在的な生産環境の危険を詳述しています。

この記事では、クエリパフォーマンスを強化するために、PostgreSQL、MySQL、MongoDBなどのさまざまなデータベースでJSON列にインデックスの作成について説明します。特定のJSONパスのインデックス作成の構文と利点を説明し、サポートされているデータベースシステムをリストします。

記事では、外部キーを使用してデータベース内の関係を表すことで、ベストプラクティス、データの完全性、および避けるべき一般的な落とし穴に焦点を当てています。

INNODBのフルテキスト検索機能は非常に強力であり、データベースクエリの効率と大量のテキストデータを処理する能力を大幅に改善できます。 1)INNODBは、倒立インデックスを介してフルテキスト検索を実装し、基本的および高度な検索クエリをサポートします。 2)一致を使用してキーワードを使用して、ブールモードとフレーズ検索を検索、サポートします。 3)最適化方法には、単語セグメンテーションテクノロジーの使用、インデックスの定期的な再構築、およびパフォーマンスと精度を改善するためのキャッシュサイズの調整が含まれます。
