Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ Distributed Database Middleware/ Best Practices/ Cross-Shard Complex SQL Tuning

Cross-Shard Complex SQL Tuning

Updated on 2025-03-17 GMT+08:00

This section describes how to optimize cross-shard complex SQL statements.

Suggestions on Tuning GROUP BY and ORDER BY

  1. If the GROUP BY field is not a data column in the table, use Generated Columns to create a column, add an index to it, and use the column as the GROUP BY condition to avoid using temporary tables for query. (Generated Columns is supported in MySQL 5.7 or later. In MySQL 5.6 and earlier versions, you can create common fields and add indexes.)
  2. Ensure that the GROUP BY and ORDER BY fields are the same. Data queries are faster when the GROUP BY field contains sharding keys.
  3. Use indexes in GROUP BY and ORDER BY.

Suggestions on Tuning JOINs

  1. Preferentially use sharding keys to perform EQUI-JOINs on conditional JOIN statements.
  2. Use the table with a smaller result set as the driving table to reduce loop JOINs. When LEFT JOINs or JOINs are used, the driving table is on the left and the driven table is on the right. When RIGHT JOINs are used, the driving table is on the right and the driven table is on the left.
  3. Add indexes to JOIN fields of the driven table. Do not join two large tables directly, especially unsharded tables without any constraint, which may use a large number of compute resources.
  4. Do not use JOINs in explicit transactions. To ensure transaction visibility, the optimal execution plan may not be used.

Suggestions on Tuning SQL UNION

  1. Do not perform UNION operations (excluding UNION ALL) between large tables, in case that the number of temporary tables exceeds the upper limit during deduplication.
  2. Do not use ORDER BY or GROUP BY in a UNION statement.

Suggestions on Tuning Temporary Tables

When complex SQL statements such as JOINs, UNION, GROUP BY, and ORDER BY are executed across shards, DDM may create a temporary table to store data. The maximum size of a temporary table is 1 million rows by default. If the number of rows in the temporary table exceeds this upper limit, an error "Temp table limit exceeded" will be displayed, indicating that the SQL statements need to be calculated again on the DDM instance, and the required temporary table size exceeds the limit allowed by the DDM instance. If such errors on temporary tables are reported, evaluate whether the SQL statements can be optimized by, for example, configuring broadcast tables, adding conditions for batch query, or modifying SQL statements based on sharding architecture. If there is no optimization potential, check data volume and instance resources. If there are no more than 3 million rows of data and instance resources are sufficient, set the instance parameter temp_table_size_limit to a large value on the console to remove the restriction on temporary table size.

Scenarios Where Temporary Tables Are Generated

  1. The GROUP BY and ORDER BY fields are different.
  2. The DISTINCT keyword is used.
  3. UNION queries are executed.
  4. Some cross-shard subqueries are executed.
  5. In a JOIN query, non-equivalent JOIN conditions are used, or a transaction is enabled on the connection between two subqueries.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback