Overview
Scenarios
Product Architecture
Instance Types
Compatibility Notes
Usage specification recommendations
SELECT c_custkey, c_name, c_mktsegment FROM customer;
SELECT o_orderkey, o_custkey, o_orderdate, o_totalprice FROM orders;
* allows you to query all columns in a table:SELECT * FROM customer;
SELECT * and explicitly specify required columns.c_comment).SELECTc_custkey AS customer_id,c_name AS customer_name,c_acctbal AS account_balanceFROM customer AS c;
WHERE clause allows you to filter query results and return only data that meets the criteria.SELECT c_custkey, c_name, c_acctbalFROM customerWHERE c_mktsegment = 'AUTOMOBILE';
SELECT o_orderkey, o_orderdate, o_totalpriceFROM ordersWHERE o_orderstatus = 'O'; -- O: Open, F: Finished, P: Pending
SELECT c_custkey, c_name, c_acctbalFROM customerWHERE c_acctbal > 5000.00;
SELECT o_orderkey, o_orderdate, o_totalpriceFROM ordersWHERE o_orderdate >= '1995-01-01';
BETWEEN for range queries:SELECT o_orderkey, o_orderdate, o_totalpriceFROM ordersWHERE o_orderdate BETWEEN '1995-01-01' AND '1995-12-31';
AND and OR to combine multiple conditions:SELECT c_custkey, c_name, c_acctbal, c_mktsegmentFROM customerWHERE c_mktsegment = 'AUTOMOBILE'AND c_acctbal > 5000.00;
LIKE for pattern matching:SELECT c_custkey, c_nameFROM customerWHERE c_name LIKE 'Customer%';
ORDER BY clause allows you to sort query results.SELECT o_orderkey, o_orderdate, o_totalpriceFROM ordersORDER BY o_orderdate;
ASC:SELECT o_orderkey, o_orderdate, o_totalpriceFROM ordersORDER BY o_orderdate ASC;
SELECT c_custkey, c_name, c_acctbalFROM customerORDER BY c_acctbal DESC;
SELECT o_orderkey, o_orderdate, o_totalpriceFROM ordersORDER BY o_orderdate DESC;
SELECT o_orderkey, o_orderdate, o_totalprice, o_orderpriorityFROM ordersORDER BY o_orderdate DESC, o_totalprice DESC;
ORDER BY to avoid filesort.ORDER BY should match the index definition order.-- Create a composite indexCREATE INDEX idx_orderdate_totalprice ON orders(o_orderdate DESC, o_totalprice DESC);-- This query can directly use the index to avoid sorting.SELECT o_orderkey, o_orderdate, o_totalpriceFROM ordersORDER BY o_orderdate DESC, o_totalprice DESC;
EXPLAIN:EXPLAIN SELECT o_orderkey, o_orderdate, o_totalpriceFROM ordersORDER BY o_orderdate DESC;
Using filesort, it indicates that index sorting is utilized, delivering optimal performance.LIMIT clause can limit the number of result rows returned, commonly used for pagination queries or obtaining Top N results.SELECT o_orderkey, o_orderdate, o_totalpriceFROM ordersORDER BY o_totalprice DESCLIMIT 10;
SELECT c_custkey, c_name, c_acctbalFROM customerORDER BY c_acctbal DESCLIMIT 20;
LIMIT offset, count or LIMIT count OFFSET offset for pagination:-- Query the second page of data (with 20 entries per page)SELECT c_custkey, c_name, c_acctbalFROM customerORDER BY c_custkeyLIMIT 20, 20; -- offset=20, count=20
-- Clearer syntaxSELECT c_custkey, c_name, c_acctbalFROM customerORDER BY c_custkeyLIMIT 20 OFFSET 20;
LIMIT 10 only needs to retrieve 10 rows of data before the results are returned.LIMIT directly at the storage engine layer.ORDER BY ... LIMIT N uses the heapsort algorithm with a space complexity of O(N).-- Without LIMIT: requires scanning and sorting the full tableSELECT o_orderkey, o_totalpriceFROM ordersORDER BY o_totalprice DESC;-- Rows scanned: 1,500,000 (full table)-- With LIMIT: early terminationSELECT o_orderkey, o_totalpriceFROM ordersORDER BY o_totalprice DESCLIMIT 10;-- Rows scanned: ~10 (using index) or with heapsort optimization
-- Poor performance: requires scanning and skipping the first 1,000,000 rowsSELECT o_orderkey, o_orderdateFROM ordersORDER BY o_orderkeyLIMIT 1000000, 20;
-- Method 1: Record the maximum ID from the last querySELECT o_orderkey, o_orderdateFROM ordersWHERE o_orderkey > 1000000 -- ID of the last record from the previous operationORDER BY o_orderkeyLIMIT 20;-- Method 2: Use covering index + deferred joinSELECT o.o_orderkey, o.o_orderdate, o.o_totalpriceFROM orders oINNER JOIN (SELECT o_orderkeyFROM ordersORDER BY o_orderkeyLIMIT 1000000, 20) tmp ON o.o_orderkey = tmp.o_orderkey;
COUNT, SUM, AVG, MAX, and MIN.SELECT COUNT(*) AS total_ordersFROM orders;
SELECT SUM(o_totalprice) AS total_salesFROM orders;
SELECTMAX(o_totalprice) AS max_order_amount,MIN(o_totalprice) AS min_order_amount,AVG(o_totalprice) AS avg_order_amountFROM orders;
GROUP BY clause to perform group statistics on data.SELECTc_mktsegment,COUNT(*) AS customer_countFROM customerGROUP BY c_mktsegmentORDER BY customer_count DESC;
SELECTYEAR(o_orderdate) AS order_year,COUNT(*) AS order_count,SUM(o_totalprice) AS total_sales,AVG(o_totalprice) AS avg_order_amountFROM ordersGROUP BY YEAR(o_orderdate)ORDER BY order_year DESC;
SELECTo_orderstatus,COUNT(*) AS status_count,SUM(o_totalprice) AS total_amountFROM ordersGROUP BY o_orderstatus;
HAVING is used to filter the results after grouping is performed (as opposed to WHERE, which filters raw rows).SELECTo_custkey,COUNT(*) AS order_count,SUM(o_totalprice) AS total_spentFROM ordersGROUP BY o_custkeyHAVING COUNT(*) > 1000ORDER BY total_spent DESC;
SELECTYEAR(o_orderdate) AS order_year,AVG(o_totalprice) AS avg_amountFROM ordersGROUP BY YEAR(o_orderdate)HAVING AVG(o_totalprice) > 10000ORDER BY order_year;
DISTINCT for deduplicated counting:SELECT COUNT(DISTINCT c_mktsegment) AS segment_countFROM customer;
SELECT COUNT(DISTINCT o_custkey) AS active_customersFROM orders;
-- Create an index on GROUP BY columnsCREATE INDEX idx_mktsegment ON customer(c_mktsegment);-- This query can utilize indexes to accelerate groupingSELECT c_mktsegment, COUNT(*)FROM customerGROUP BY c_mktsegment;
GROUP BY columns are the prefix of the index, TDSQL Boundless can skip irrelevant index entries.EXPLAIN whether Using index for group-by is utilized.-- Create a covering indexCREATE INDEX idx_orderdate_totalprice ON orders(o_orderdate, o_totalprice);-- This query avoids table lookup and reads directly from the index.SELECTYEAR(o_orderdate) AS order_year,SUM(o_totalprice) AS total_salesFROM ordersGROUP BY YEAR(o_orderdate);
-- Assume the orders table is partitioned by year-- This query only scans the 1995 partitionSELECTCOUNT(*) AS order_count,SUM(o_totalprice) AS total_salesFROM ordersWHERE o_orderdate BETWEEN '1995-01-01' AND '1995-12-31';
Esta página foi útil?
Você também pode entrar em contato com a Equipe de vendas ou Enviar um tíquete em caso de ajuda.
comentários