This topic describes the writing specifications for the SELECT statement in OceanBase Database.
It is recommended to specify field names in the SELECT statement. Writing "*" is prohibited.
Increases the parsing cost of the query analyzer.
The addition and deletion of fields are prone to inconsistency with the resultMap configuration.
We recommend that you remove unnecessary columns from the SELECT statement. In an SQL statement, only include the necessary columns in the SELECT clause. This is because including unnecessary columns will cause the database to perform a table access (accessing the data page to retrieve the specific requested column), which results in more I/O operations.
In SQL statements that use partitioned tables, the partitioning key must be included in all predicates.
We recommend that you do not use
count(column_name)orcount(constant)to replacecount(*).Thecount(*)syntax is a standard SQL:1992 statement for counting rows, which is database-independent and does not depend on whether the data is NULL or non-NULL.Note
count(*)counts rows with NULL values, whereascount(column_name)does not count rows where the column is NULL.Avoid using the UNION clause in SELECT statements unless necessary. It is recommended to use UNION ALL instead, and limit the number of UNION clauses to five.
Note
UNION automatically performs a final sort to eliminate duplicates, but in practice, duplicate rows are usually absent between different queries.
The
INoperation is not recommended. Subqueries withINshould be rewritten asJOINoperations whenever possible.Reading locks with
SELECT ... for update (with cs)are not recommended. If a transaction is large, it can easily lead to lock waits under high concurrency, impacting business operations.Using non-equivalent columns in an equality query is prohibited.
Note
If the field type is VARCHAR and the value in the WHERE condition is an INT, an implicit conversion occurs, which may lead to an out-of-bounds access and potentially crash the database.
In an ORDER BY clause, the fields must be unique or uniquely combinable (for non-unique fields, the sorting behavior differs between OceanBase Database and MySQL).
It is not recommended to include complex queries such as computation operations, multi-table joins, or table traversal CASE WHEN statements in the SQL for core business processes. It is suggested to split these into simpler single-table queries.
For SQL statements with poor query condition filtering, an emergency workaround is to bind the execution plan to the SQL. However, for long-term use, it is recommended to modify the SQL logic. Excessive use of hints may impact subsequent upgrades and maintenance.
For queries with DISTINCT, ORDER BY, and GROUP BY clauses, the intermediate result set is limited to within 10,000 rows. For large result sets (intermediate result sets exceeding 10,000 rows), sorting and grouping are implemented on the application side.
When setting the number of parallel tasks, the optimal value is the number of shards in the table.