PolarDB allows you to use Object Storage Service (OSS) foreign tables to directly query CSV-formatted data stored in OSS. This effectively reduces the storage costs. This topic describes how to use OSS foreign tables to access OSS data.
Prerequisites
Your PolarDB cluster meets one of the following requirements:
If you use a PolarDB for MySQL 8.0.1 cluster, the revision version of the cluster must be 8.0.1.1.25.4 or later.
If you use a PolarDB for MySQL 8.0.2 cluster, the revision version of the cluster must be 8.0.2.2.1 or later.
For more information about how to check the cluster version, see the "Query the engine version" section of the Engine versions topic.
How it works
You can use OSS foreign tables to store CSV-formatted cold data in an OSS bucket for query and analysis. Cold data refers to data that is infrequently accessed. The following figure shows the process.
CSV-formatted data can contain numeric, date and time, and string values, as well as NULL values. The following tables describe the supported data types.
Geospatial data types are not supported.
You cannot query compressed files in the CSV format.
NULL values are supported for a PolarDB for MySQL 8.0.1 cluster whose revision version is 8.0.1.1.28 or later, or a PolarDB for MySQL 8.0.2 cluster whose revision version is 8.0.2.2.5 or later.
Numeric types
Data type
Size
Data range (signed)
Data range (unsigned)
Description
TINYINT
1 Byte
-128~127
0~255
A small integer value
SMALLINT
2 bytes
-32768~32767
0~65535
An integer value
MEDIUMINT
3 bytes
-8388608~8388607
0~16777215
An integer value
INT or INTEGER
4 bytes
-2147483648~2147483647
0~4294967295
An integer value
BIGINT
8 bytes
-9,223,372,036,854,775,808~9223372036854775807
0~18446744073709551615
A big integer value
FLOAT
4 bytes
-3.402823466 E+38~-1.175494351E-38; 0; 1.175494351E-38~3.402823466351E+38
0; 1.175494351E-38~3.402823466E+38
A single-precision floating-point value
DOUBLE
8 bytes
-2.2250738585072014E-308~-1.7976931348623157E+308; 0; 1.7976931348623157E+308~2.2250738585072014E-308
0; 1.7976931348623157E+308~2.2250738585072014E-308
A double-precision floating-point value
DECIMAL
For DECIMAL(M,D), it is M+2 if M>D. Otherwise, it is D+2.
Depends on the values of M and D.
Depends on the values of M and D.
A decimal value
Date and time types
Data type
Size
Data range
Format
Description
DATE
3 bytes
1000-01-01~9999-12-31
YYYY-MM-DD
A date value
TIME
3 bytes
-838:59:59~838:59:59
HH:MM:SS
A time value or duration
YEAR
1 Byte
1901~2155
YYYY
A year value
DATETIME
8 bytes
1000-01-01 00:00:00~9999-12-31 23:59:59
YYYY-MM-DD HH:MM:SS
A combined date and time value
NoteThe month and date of this type must have two digits. For example, January 1, 2020 must be written as 2020-01-01, instead of 2020-1-1. The query cannot be executed as expected if 2020-1-1 is pushed down to OSS.
TIMESTAMP
4 bytes
1970-01-01 00:00:00~2038-01-19 03:14:07
YYYY-MM-DD HH:MM:SS
A timestamp (combined date and time) value
NoteThe month and date of this type must have two digits. For example, January 1, 2020 must be written as 2020-01-01, instead of 2020-1-1. The query cannot be executed as expected if 2020-1-1 is pushed down to OSS.
String types
Data type
Size
Description
CHAR
0~255 bytes
A fixed-length string
VARCHAR
0~65535 bytes
A variable-length string
TINYBLOB
0~255 bytes
A small binary large object up to 255 characters
TINYTEXT
0~255 bytes
A short string
BLOB
0~65535 bytes
A standard binary large object
TEXT
0~65535 bytes
A standard string
MEDIUMBLOB
0~16777215 bytes
A medium binary large object
MEDIUMTEXT
0~16777215 bytes
A medium string
LONGBLOB
0~4294967295 bytes
A long binary large object
LONGTEXT
0~4294967295 bytes
A long string
NULL values
Insert a NULL value.
Insert a NULL value into an OSS foreign table.
To insert a NULL value into an OSS foreign table, you must specify
NULL_MARKER
when you create the OSS foreign table. The default value ofNULL_MARKER
is NULL for an OSS foreign table. You can execute theshow create table
statement to check the value of NULL_MARKER.show create table t1;
Result:
show create table t1; +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int(11) DEFAULT NULL ) ENGINE=CSV DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci /*!99990 800020204 NULL_MARKER='NULL' */ CONNECTION='server_name' | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
Insert a NULL value into a CSV file.
If you insert a value of
NULL_MARKER
for a field in a CSV file without enclosing the value in double quotation marks ("), PolarDB identifies the value as NULL.NoteIf you enclose the value of
NULL_MARKER
in double quotation marks ("), PolarDB identifies the value as a string. This means that theis_null
statement cannot identify the value as NULL. If the data type of a field to which a NULL value is assigned in the CSV file does not match that of the corresponding field in the OSS foreign table, an error is reported.The value of cannot consist solely of digits or be left empty. In addition, it cannot contain the following characters:
"
,\n
,\r
, and,
Example: Execute the following statement to create an OSS foreign table:
CREATE TABLE `t1` ( `id` int(11) DEFAULT NULL, `name` varchar(20) DEFAULT NULL, `time` timestamp NULL DEFAULT NULL ) ENGINE=CSV NULL_MARKER='NULL' CONNECTION='server_name';
In this example, the data file contains the following data:
1,"xiaohong","2022-01-01 00:00:00" NULL,"xiaoming","2022-02-01 00:00:00" 3,NULL,"2022-03-01 00:00:00" 4,"xiaowang",NULL
The following OSS data is obtained if you query the OSS foreign table:
select * from t1; +------+----------+---------------------+ | id | name | time | +------+----------+---------------------+ | 1 | xiaohong | 2022-01-01 00:00:00 | | NULL | xiaoming | 2022-02-01 00:00:00 | | 3 | NULL | 2022-03-01 00:00:00 | | 4 | xiaowang | NULL | +------+----------+---------------------+ 4 rows in set (0.00 sec)
Read a NULL value.
When data is read from a CSV file, if the value of a field in the CSV file is NULL and the corresponding value in the OSS foreign table can be set to NULL, the field is directly set to NULL.
When data is read from a CSV file, if the value of a field in the CSV file is NULL but the corresponding value in the OSS foreign table is set to NOT NULL, the data in the CSV file conflicts with the data specified in the OSS foreign table. In this case, different results are returned depending on the specified syntax verification rule.
If you set the
sql_mode
parameter toSTRICT_TRANS_TABLES
, an error is reported.If you set the
sql_mode
parameter to a value other thanSTRICT_TRANS_TABLES
and the field has a default value, the default value of the field is used. If the field does not have a default value, a MySQL default value is assigned to the field based on the field type. For more information, see Data Type Default Values. If a warning message appears, you can execute theshow warnings;
statement to view the details of the warning message.
NoteYou can execute the
show variables like "sql_mode";
statement to view the current syntax verification rule. You can also change the syntax verification rule by modifying the value of thesql_mode
parameter on the Parameters page in the PolarDB console. For more information, see Configure cluster and node parameters.Example: Create an OSS foreign table named
t
and set theid
field to NOT NULL. Do not specify a default value for the field.CREATE TABLE `t` ( `id` int(11) NOT NULL ) ENGINE=CSV CONNECTION="server_name";
In this example, the
t.CSV
file contains the following data:NULL 2
When you use an OSS foreign table to read data from the CSV file, one of the following scenarios occurs:
If you set the
sql_mode
parameter toSTRICT_TRANS_TABLES
and execute the following statement to query data in the CSV file:select * from t;
The following error message is reported:
ERROR 1364 (HY000): Field 'id' doesn't have a default value
If you set the
sql_mode
parameter to a value other thanSTRICT_TRANS_TABLES
and execute the following statement to query data in the CSV file:select * from t;
The following result is returned:
+----+ | id | +----+ | 0 | | 2 | +----+ 2 rows in set, 1 warning (0.00 sec)
0 is the default value of MySQL.
Execute the following statement to view the details of the warning message:
show warnings;
Result:
+---------+------+-----------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------+ | Warning | 1364 | Field 'id' doesn't have a default value | +---------+------+-----------------------------------------+ 1 row in set (0.00 sec)
Limits
You can use OSS foreign tables to query only CSV-formatted data.
You can perform only the following operations on OSS foreign tables: CREATE, SELECT, and DROP.
NoteThe DROP operation deletes only the table information in PolarDB, without affecting the data files stored in OSS.
OSS foreign tables do not support indexing, partitioning, or transactions.
Parameters
You can view or modify the following parameters on the Parameters page of the console.
Parameter | Category | Description |
loose_csv_oss_buff_size | Session parameter | The size of memory occupied by an OSS thread. Default value: 134217728. Unit: bytes. Valid values: 4096 to 134217728. |
loose_csv_max_oss_threads | Global parameter | The number of OSS threads that are allowed to run. Default value: 1. Valid values: 1 to 100. |
You can calculate the total memory usage of OSS by using the following formula:Total memory usage of OSS = Value of the loose_csv_max_oss_threads parameter × Value of the loose_csv_oss_buff_size parameter
.
When OSS is used, we recommend that you limit the total memory usage of OSS to 5% of the memory capacity of the current node. Otherwise, an out-of-memory issue may occur.
Procedure
Upload a CSV file to OSS.
You can use the ossutil tool to upload a local CSV file to a remote OSS bucket.
NoteThe OSS directory to which the CSV file is uploaded must be the same as the directory specified by the
my_database_name
oross_prefix
parameter on the OSS server.The name of the uploaded CSV file must be in the
Foreign table name.CSV
format, and the .CSV extension must be in uppercase. For example, if the name of the created OSS foreign table ist1
, the name of the uploaded CSV file must bet1.CSV
.The data fields in the CSV file must match the fields of the OSS foreign table. For example, if the created OSS foreign table
t1
contains only one fieldid
and the field is of theINT
type, the uploaded CSV file must contain only one field of theINT
type.We recommend that you directly upload the local MySQL data file and create an OSS foreign table based on the table definitions.
Add OSS connection information.
You can add OSS connection information by creating an OSS server.
NoteOther methods to connect to OSS have been disabled due to security risks. You can only create an OSS server to add OSS connection information and connect to OSS.
If you use a PolarDB for MySQL 8.0.1 cluster whose revision version is 8.0.1.1.28 or later, or a PolarDB for MySQL 8.0.2 cluster whose revision version is 8.0.2.2.5 or later, you can create an OSS server by using the following syntax:
CREATE SERVER <server_name> FOREIGN DATA WRAPPER oss OPTIONS ( EXTRA_SERVER_INFO '{"oss_endpoint": "<my_oss_endpoint>","oss_bucket": "<my_oss_bucket>","oss_access_key_id": "<my_oss_access_key_id>","oss_access_key_secret": "<my_oss_access_key_secret>","oss_prefix":"<my_oss_prefix>","oss_sts_token":"<my_oss_sts_token>"}' );
NoteOn a PolarDB for MySQL 8.0.1 cluster whose revision version is 8.0.1.1.29 or later or a PolarDB for MySQL 8.0.2 cluster whose revision version is 8.0.2.2.6 or later, you can use the
my_oss_sts_token
parameter.The
my_database_name
parameter is supported in the syntax. If both themy_database_name
andmy_oss_prefix
parameters exist on the OSS server that you create, the final path of the file ismy_oss_prefix/my_database_name
. For more information about how to specify themy_database_name
parameter, see the following text.
The following table describes the parameters in the syntax.
Parameter
Type
Required
Description
server_name
String
Yes
The OSS server name.
NoteThe name must be globally unique. The name can be up to 64 characters in length and is not case-sensitive. A name that contains more than 64 characters is automatically truncated. You can specify the OSS server name as a quoted string.
my_oss_endpoint
String
Yes
The endpoint of the OSS server.
NoteIf you access your database from an Alibaba Cloud server, use an internal endpoint to prevent incurring Internet traffic. An internal endpoint contains the keyword "internal".
For example, the internal endpoint of an OSS node in the China (Hangzhou) region is
oss-cn-xxx-internal.aliyuncs.com
.my_oss_bucket
String
Yes
The bucket where the data file is stored. You must create the bucket on OSS before you import data.
NoteWe recommend that you deploy the bucket in the same zone as the PolarDB cluster to reduce network latency.
my_oss_access_key_id
String
Yes
The AccessKey ID of the Resource Access Management (RAM) user or Alibaba Cloud account.
For more information about how to create an AccessKey pair, see Create an AccessKey pair.
my_oss_access_key_secret
String
Yes
The AccessKey secret of the RAM user or Alibaba Cloud account.
For more information about how to create an AccessKey pair, see Create an AccessKey pair.
my_oss_prefix
String
No
The OSS directory for the CSV file.
my_oss_sts_token
String
No
The temporary access credentials provided by Security Token Service (STS)
NoteThis parameter is required if you use a temporary access credential provided by STS to access OSS.
The value of the
my_oss_sts_token
parameter has a default expiration time. If the value of themy_oss_sts_token
parameter expires, execute the following statement to reset all parameters inEXTRA_SERVER_INFO
:ALTER SERVER server_name OPTIONS(EXTRA_SERVER_INFO '{"oss_endpoint": "<my_oss_endpoint>", "oss_bucket": "<my_oss_bucket>", "oss_access_key_id": "<my_oss_access_key_id>", "oss_access_key_secret": "<my_oss_access_key_secret>", "oss_prefix":"<my_oss_prefix>", "oss_sts_token": "<my_oss_sts_token>"}');
NoteThe SERVERS_ADMIN permissions are required when you create an OSS server. You can execute the
SHOW GRANTS FOR the current user
statement to check whether the current user has the SERVERS_ADMIN permissions. A privileged account has the SERVERS_ADMIN permissions by default, and can grant the SERVERS_ADMIN permissions to standard accounts.If you do not have the SERVERS_ADMIN permissions, the error message
Access denied; you need (at least one of) the SERVERS_ADMIN OR SUPER privilege(s) for this operation
appears.If you use a standard account that does not have the SERVERS_ADMIN permissions, you can use a privileged account to execute the following statement:
GRANT SERVERS_ADMIN ON *.* TO `users`@`%` WITH GRANT OPTION
. If you use a privileged account that does not have the SERVERS_ADMIN permissions, you can reset the permissions of the account. To do so, find the cluster in the console and click the cluster ID or name to go to the cluster details page. In the left-side navigation pane, choose Settings and Management > Accounts. On the User Account tab, find the privileged account that you want to manage and click Reset Permissions in the Actions column. Wait until the permissions are reset. Then, the privileged account has the SERVERS_ADMIN permissions.If you use a privileged account, you can execute the
SELECT Server_name, Extra_server_info FROM mysql.servers;
statement to view the information about the OSS server that you create. The values of theoss_access_key_id
andoss_access_key_secret
parameters are encrypted for security reasons.
If you use a PolarDB for MySQL 8.0.1 cluster whose revision version is from 8.0.1.1.25.4 to 8.0.1.1.28, or a PolarDB for MySQL 8.0.2 cluster whose revision version is from 8.0.2.2.1 to 8.0.2.2.5, you can create an OSS server by using the following syntax:
CREATE SERVER <server_name> FOREIGN DATA WRAPPER oss OPTIONS (DATABASE '<my_database_name>', EXTRA_SERVER_INFO '{"oss_endpoint": "<my_oss_endpoint>","oss_bucket": "<my_oss_bucket>","oss_access_key_id":"<my_oss_access_key_id>","oss_access_key_secret":"<my_oss_access_key_secret>"}' );
NoteThe
oss_prefix
parameter is not supported in the syntax.The following table describes the parameters in the syntax.
Parameter
Type
Required
Description
server_name
String
Yes
The OSS server name.
NoteThe name must be globally unique. The name can be up to 64 characters in length and is not case-sensitive. A name that contains more than 64 characters is automatically truncated. You can specify the OSS server name as a quoted string.
my_database_name
String
No
The name of the OSS directory for the CSV file.
my_oss_endpoint
String
Yes
The endpoint of the OSS server.
NoteIf you access your database from an Alibaba Cloud server, use an internal endpoint to prevent incurring Internet traffic. An internal endpoint contains the keyword "internal".
Example:
oss-cn-xxx-internal.aliyuncs.com
.my_oss_bucket
String
Yes
The bucket where the data file is stored. You must create the bucket on OSS before you import data.
my_oss_access_key_id
String
Yes
The AccessKey ID of the RAM user or Alibaba Cloud account.
For more information about how to create an AccessKey pair, see Create an AccessKey pair.
my_oss_access_key_secret
String
Yes
The AccessKey secret of the RAM user or Alibaba Cloud account.
For more information about how to create an AccessKey pair, see Create an AccessKey pair.
Create an OSS foreign table.
After you define an OSS server, you can create an OSS foreign table on PolarDB to connect to OSS. Example:
create table t1 (id int) engine=csv connection="connection_string";
The value of
connection_string
consists of the following items that are separated by forward slashes (/):The OSS server name.
Optional. The path of the data file in OSS.
NoteIf you use a PolarDB for MySQL 8.0.1 cluster whose revision version is 8.0.1.1.28 or later, or a PolarDB for MySQL 8.0.2 cluster whose revision version is 8.0.2.2.5 or later, you can configure the path of the data file in OSS.
Optional. The data file name.
NoteThe data file name cannot contain the
.CSV
extension.
Example:
create table t1 (id int) engine=csv connection="server_name/a/b/c/d/t1";
In this example, the path of the data file in OSS is
oss_prefix/a/b/c/d/
, and the name of the data file ist1.CSV
.NoteYou can use only the data file name to specify the data file that corresponds to the OSS foreign table. For example, if you execute the
create table t1 (id int) engine=csv connection="server_name/t2"
statement, PolarDB searches for thet2.CSV
file in the directory specified by the oss_prefix parameter in OSS.If you add the path of the data file in OSS to
connection_string
, you must add the name of the data file. Otherwise, the last segment of the path is considered as the file name when PolarDB searches for the data file.If you do not specify a data file name, the OSS file corresponding to the current table is
Name of the current table.CSV
. If you specify a data file name, the OSS file corresponding to the current table isSpecified data file name.CSV
.
After an OSS foreign table is created, you can execute the
show create table
statement to view the table. Check whether the engine of the created table is CSV. If not, the version of your current PolarDB cluster is outdated and does not support OSS.Query data.
In this example, the
t1
table mentioned in the previous steps is used to show how to query data.# Query the number of data records in the t1 table. SELECT count(*) FROM t1; # Query data records in the specified range. SELECT id FROM t1 WHERE id < 10 AND id > 1; # Query a specific record. SELECT id FROM t1 where id = 3; # Query records by joining multiple tables. SELECT id FROM t1 left join t2 on t1.id = t2.id WHERE t2.name like "%er%";
The following table describes the common error messages and causes when you query data.
NoteIf no error message is reported but a warning message appears when you query data, you must execute the
SHOW WARNINGS;
statement to view the message.Error message
Cause
Solution
OSS error: No corresponding data file on the OSS engine.
The specified data file is not found in OSS.
Check whether the data file exists in the specified path in OSS based on the preceding rules.
If yes, check whether the data file complies with the following naming rules: The data file name is in the
Foreign table name.CSV
format, and the .CSV extension is in uppercase.If not, you must upload the data file to the specified path.
There is not enough memory space for OSS transmission. Currently requested memory %d.
Insufficient memory for OSS queries.
You can use one of the following methods to fix this error:
On the Parameters page in the console, modify the value of the
loose_csv_max_oss_threads
parameter to run more OSS threads.Execute the flush table statement to close threads for some OSS tables.
ERROR 8054 (HY000): OSS error: error message : Couldn't connect to server. Failed connect to aliyun-mysql-oss.oss-cn-hangzhou-internal.aliyuncs.com:80;
The current cluster cannot connect to the OSS server.
Check whether the current cluster is in the same zone as the OSS bucket.
If not, you must move the current cluster and the OSS bucket to the same zone.
If yes, you can change the endpoint of the cluster to a public endpoint. If the error persists after the endpoint is modified, contact Alibaba Cloud technical support.
Query optimization
During the query process, the query engine can push queries with specific conditions down to a remote OSS bucket to enhance query efficiency. This optimization is called engine condition pushdown
. The engine condition pushdown feature is subject to the following limits:
Only UTF-8 encoded CSV files are supported.
Only the following types of operators are supported in SQL statements:
Comparison operators:
>
,<
,>=
,<=
, and==
Logical operators:
LIKE
,IN
,AND
, andOR
Arithmetic operators:
+
,-
,*
, and/
Only a single file can be queried when you use an SQL statement. The following clauses are not supported: JOIN, ORDER BY, GROUP BY, and HAVING.
The WHERE clause cannot contain aggregation conditions. For example,
WHERE max(age) > 100
is not allowed.A maximum of 1,000 columns can be specified for an SQL statement. The column name in an SQL statement can be a maximum of 1,024 bytes in length.
A maximum of five wildcards (
%
) are supported in a LIKE clause.A maximum of 1,024 constants are supported in an IN clause.
The maximum column size and row size for a CSV object are 256 KB.
The maximum size of an SQL statement is 16 KB. A maximum of 20 expressions can be added after a WHERE clause. Each statement supports up to 100 aggregation operations.
By default, the engine condition pushdown feature is disabled. To enable this feature, execute the SET SESSION optimizer_switch='engine_condition_pushdown=on';
statement.
Queries that meet the preceding conditions are pushed down to a remote OSS bucket. You can use the execution plan of an OSS foreign table to view the queries that are pushed down to a remote OSS bucket.
View the execution plan of an OSS foreign table by executing the
EXPLAIN
statement. Example:EXPLAIN SELECT count(*) FROM `t1` WHERE `id` > 5 AND `id` < 100 AND `name` LIKE "%1%%%%%" GROUP BY `id` ORDER BY `id` DESC; +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+----------------------------------------------------------------------------------------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+----------------------------------------------------------------------------------------------------------------------------------+ | 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 15000 | 1.23 | Using where; With pushed engine condition ((`test`.`t1`.`id` > 5) and (`test`.`t1`.`id` < 100)); Using temporary; Using filesort | +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+----------------------------------------------------------------------------------------------------------------------------------+ 1 row in set, 1 warning (0.00 sec)
Queries with the conditions after
With pushed engine condition
can be pushed down to a remote OSS bucket. Queries with the conditions`name` LIKE "%1%%%%%"
andGROUP BY `id` ORDER BY `id` DESC
can be performed only on the local OSS server.View the execution plan of an OSS foreign table in the
tree
format. Example:EXPLAIN FORMAT=tree SELECT SELECT count(*) FROM `t1` WHERE `id` > 5 AND `id` < 100 AND `name` LIKE "%1%%%%%" Y `id` ORDER BY `id` DESC; +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | EXPLAIN | +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | -> Sort: <temporary>.id DESC -> Table scan on <temporary> -> Aggregate using temporary table -> Filter: (t1.`name` like '%1%%%%%') (cost=1690.00 rows=185) -> Table scan on t1, extra ( engine conditions: ((t1.id > 5) and (t1.id < 100)) ) (cost=1690.00 rows=15000) | +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
Queries with the conditions after
engine conditions:
can be pushed down to a remote OSS bucket. Queries with the conditions`name` LIKE "%1%%%%%"
andGROUP BY `id` ORDER BY `id` DESC
can be performed only on the local OSS server.NoteYou must use a PolarDB for MySQL 8.0.2 cluster to query data. You can query the version number to check the cluster version.
View the execution plan of an OSS foreign table in the
JSON
format. Example:EXPLAIN FORMAT=json SELECT count(*) FROM `t1` WHERE `id` > 5 AND `id` < 100 AND `name` LIKE "%1%%%%%" GROUP BY `id` ORDER BY `id` DESC; +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | EXPLAIN | +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | { "query_block": { "select_id": 1, "cost_info": { "query_cost": "1875.13" }, "ordering_operation": { "using_filesort": false, "grouping_operation": { "using_temporary_table": true, "using_filesort": true, "cost_info": { "sort_cost": "185.13" }, "table": { "table_name": "t1", "access_type": "ALL", "rows_examined_per_scan": 15000, "rows_produced_per_join": 185, "filtered": "1.23", "engine_condition": "((`test`.`t1`.`id` > 5) and (`test`.`t1`.`id` < 100))", "cost_info": { "read_cost": "1671.49", "eval_cost": "18.51", "prefix_cost": "1690.00", "data_read_per_join": "146K" }, "used_columns": [ "id", "name" ], "attached_condition": "(`test`.`t1`.`name` like '%1%%%%%')" } } } } } | +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set, 1 warning (0.00 sec)
Queries with the conditions after
engine conditions:
can be pushed down to a remote OSS bucket. Queries with the conditions`name` LIKE "%1%%%%%"
andGROUP BY `id` ORDER BY `id` DESC
can be performed only on the local OSS server.
If the following error occurs, some characters in the current OSS data file do not meet the requirements for engine condition pushdown.
OSS error: The current query does not support engine condition pushdown. You need to use NO_ECP() hint or set optimizer_switch = 'engine_condition_pushdown=OFF' to turn off the condition push down function.
You can use hints or the optimizer_switch variable to manually disable the engine condition pushdown feature.
Use hints
Use a hint to disable the engine condition pushdown feature for a query. In the following example, the engine condition pushdown feature is disabled for the
t1
table:SELECT /*+ NO_ECP(t1) */ `j` FROM `t1` WHERE `j` LIKE "%c%" LIMIT 10;
Use the optimizer_switch variable
Use the optimizer_switch variable to disable the engine condition pushdown feature for all queries in the current session.
SET SESSION optimizer_switch='engine_condition_pushdown=off'; # Set the engine_condition_pushdown parameter to off. In this case, the engine condition pushdown feature is disabled for all queries in the current session.
You can execute the following statement to determine whether the engine condition pushdown feature is enabled for all queries in the current session based on the value of the optimizer_switch variable:
select @@optimizer_switch;
Synchronize OSS server information between multiple nodes
The primary and read-only nodes of a PolarDB cluster share the same OSS server. This ensures that these nodes can access OSS data. The synchronization of OSS server information between these nodes is lock-free to ensure that operations on these nodes are independent.
After you modify the OSS server information, the modifications are synchronized to read-only nodes in a lock-free manner. If a thread on a read-only node holds the lock for the OSS server, the synchronization of OSS server information may be delayed. In this case, you can execute the /*force_node='pi-bpxxxxxxxx'*/ flush privileges;
or /*force_node='pi-bpxxxxxxxx'*/flush table oss_foreign_table;
statement to manually update the OSS server information of read-only nodes.