When PolarDB for MySQL queries are slow, you can use Data Transmission Service (DTS) to synchronize production data in real time to an Alibaba Cloud Elasticsearch cluster for search and analytics. This solution suits scenarios that require high-performance real-time synchronization from relational databases.
Background information
The following cloud services are used:
-
Data Transmission Service (DTS): A data streaming service that integrates data migration, data subscription, and real-time data synchronization. DTS supports synchronizing
INSERT,DELETE, andUPDATEoperations. For more information, see Data Transmission Service (DTS) and Overview of data synchronization scenarios. -
PolarDB is a next-generation relational database service developed by Alibaba Cloud, compatible with MySQL, PostgreSQL, and Oracle. A PolarDB cluster supports up to 100 TB of storage and can scale to 16 nodes. For more information, see PolarDB for MySQL overview.
-
Elasticsearch is a Lucene-based, distributed, real-time search and analytics engine that stores, queries, and analyzes large datasets in near real time. For more information, see What is Alibaba Cloud Elasticsearch?
Precautions
-
DTS does not synchronize data changes generated by DDL operations. If a DDL operation is performed on a table in the source database during data synchronization, you must perform the following operations: Remove the table from the data synchronization task, remove the index for the table from the Elasticsearch cluster, and then add the table to the data synchronization task again. For more information, see Remove an object from a data synchronization task and Add an object to a data synchronization task.
-
If you want to add columns to the source table, modify the mappings of the index that corresponds to the table. Then, perform the related DDL operation on the source table, pause the data synchronization task, and start the task again.
-
Full data synchronization consumes read and write resources on both the source and destination databases, which can increase database load. If your database has poor performance, low specifications, or heavy workloads (for example, slow SQL queries, tables without primary keys, or destination deadlocks), the load increase may cause service unavailability. Evaluate database performance beforehand and perform data synchronization during off-peak hours when CPU utilization of both databases is below 30%.
-
If a full data synchronization task fails during peak hours, you can restart it.
-
If you perform incremental data synchronization during peak hours, data synchronization latency may occur.
-
Process
To synchronize data, perform the following steps:
-
Make preparations: Add the data to be synchronized to the source PolarDB for MySQL database, create an Alibaba Cloud Elasticsearch cluster, and enable the Auto Indexing feature for the Elasticsearch cluster.
-
Create and run a data synchronization task: Create and run a data synchronization task in the DTS console. Then, full and incremental data is automatically synchronized.
Step 1: Make preparations
In this example, a PolarDB for MySQL 8.0.1 cluster of Enterprise Edition and an Alibaba Cloud Elasticsearch V7.10 cluster are prepared.
Prepare the source database and the data to be synchronized
-
Create a PolarDB for MySQL 8.0.1 cluster of Enterprise Edition. For more information, see Purchase an Enterprise Edition cluster.
-
The binary logging feature is enabled for the PolarDB for MySQL cluster. For more information, see Enable binary logging.
In the left-side navigation pane, choose Settings and Management > Binlog Management. On the Binlog Management page, click Enable Now.
-
Create an account and a database named
test_polardb. For more information, see Create and manage database accounts and Manage databases. -
In the
test_polardbdatabase, create a table namedproductand insert data.-
Create a table
CREATE TABLE `product` ( `id` bigint(32) NOT NULL AUTO_INCREMENT, `name` varchar(32) NULL, `price` varchar(32) NULL, `code` varchar(32) NULL, `color` varchar(32) NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8; -
Insert test data into the table
INSERT INTO `test_polardb`.`product` (`id`,`name`,`price`,`code`,`color`) VALUES (1,'mobile phone A','2000','amp','golden'); INSERT INTO `test_polardb`.`product` (`id`,`name`,`price`,`code`,`color`) VALUES (2,'mobile phone B','2200','bmp','white'); INSERT INTO `test_polardb`.`product` (`id`,`name`,`price`,`code`,`color`) VALUES (3,'mobile phone C','2600','cmp','black'); INSERT INTO `test_polardb`.`product` (`id`,`name`,`price`,`code`,`color`) VALUES (4,'mobile phone D','2700','dmp','red'); INSERT INTO `test_polardb`.`product` (`id`,`name`,`price`,`code`,`color`) VALUES (5,'mobile phone E','2800','emp','silvery');
-
Prepare the destination Elasticsearch cluster
-
Create an Alibaba Cloud Elasticsearch V7.10 cluster. For more information, see Create an Alibaba Cloud Elasticsearch cluster.
-
Enable the Auto Indexing feature for the Elasticsearch cluster. For more information, see Configure the YML file.
On the Settings and Management > Cluster Configuration page of the instance, find the Auto Indexing configuration item in the YML File Configuration section, and click Modify Configuration on the right to enable it.
Step 2: Create and run a data synchronization task
-
Log on to the DTS data synchronization task list page (new version).
-
Click Create Task.
-
On the page that appears, create and configure a data synchronization task as prompted.
For information about the parameters that are involved in the following steps, see Synchronize data from a PolarDB for MySQL cluster.
-
Configure the source and destination databases, and then click Test Connectivity and Proceed.
Source database information:
-
Set Database Type to PolarDB for MySQL
-
Set Access Method to Cloud Instance, and set Instance Region to China (Hangzhou)
-
Set Cross-account to No
-
Select the PolarDB Instance ID, and enter the Database Account and Database Password
-
Set Connection Type to Non-encrypted Connection
Destination database information:
-
Set Database Type to Elasticsearch
-
Set Access Method to Cloud Instance, and set Instance Region to China (Hangzhou)
-
Set Type to Cluster
-
Select the Instance ID, set Database Account to elastic, and enter the Database Password
-
-
Configure the objects from which you want to synchronize data.
-
Synchronization Type: Select Schema Synchronization, Full Data Synchronization, and Incremental Data Synchronization
-
Index Name: Select TableName
-
Processing Mode of Conflicting Tables: Select Pre-check and Report Errors
-
Case Sensitivity Policy for Object Names in the Destination Database: Select DTS Default Policy
-
In the Source Objects section, move the product table in the test_polardb database to the Selected Objects section
-
-
Configure advanced settings. In this example, the default advanced settings are used.
-
On the Database/Table/Column Settings page, click Set All to Non-_routing Policy.
If the destination Elasticsearch cluster runs a 7.x version, you must set the routing policy to non-routing for all tables.
-
-
After the configuration is complete, save the data synchronization task, perform a pre-check on the task, and purchase a DTS instance to start the data synchronization task.
After you purchase the DTS instance, the data synchronization task starts automatically. You can view progress on the Data Synchronization page. After full data synchronization completes, you can query the synchronized data in the Elasticsearch cluster.
In the Running Phase Information column, you can view the synchronization progress, which includes three phases: Schema Synchronization, Full Data Synchronization, and Incremental Data Synchronization. A green check mark is displayed after each phase is complete.
Step 3 (Optional): Verify the data synchronization results
-
Log on to the Kibana console of the Elasticsearch cluster.
For more information, see Log on to the Kibana console.
-
On the Kibana page, go to . In the Console, run the following commands.
-
Verify the synchronization result of full data.
Run the following command:
GET /product/_searchIf the command is successfully run, the following result is returned:
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 5, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "product", "_type" : "product", "_id" : "3", "_score" : 1.0, "_source" : { "id" : 3, "name" : "mobile phone C", "price" : "2600", "code" : "cmp", "color" : "black" } }, { "_index" : "product", "_type" : "product", "_id" : "5", "_score" : 1.0, "_source" : { "id" : 5, "name" : "mobile phone E", "price" : "2800", "code" : "emp", "color" : "silvery" } }, { "_index" : "product", "_type" : "product", "_id" : "4", "_score" : 1.0, "_source" : { "id" : 4, "name" : "mobile phone D", "price" : "2700", "code" : "dmp", "color" : "red" } }, { "_index" : "product", "_type" : "product", "_id" : "2", "_score" : 1.0, "_source" : { "id" : 2, "name" : "mobile phone B", "price" : "2200", "code" : "bmp", "color" : "white" } }, { "_index" : "product", "_type" : "product", "_id" : "1", "_score" : 1.0, "_source" : { "id" : 1, "name" : "mobile phone A", "price" : "2000", "code" : "amp", "color" : "golden" } } ] } } -
Verify the synchronization result of incremental data.
-
Execute the following statement to insert a data record into the source table:
INSERT INTO `test_polardb`.`product` (`id`,`name`,`price`,`code`,`color`) VALUES (6,'mobile phone F','2750','fmp','white'); -
After incremental data synchronization is complete, run the
GET /product/_searchcommand again to view the result.If the command is successfully run, the following result is returned:
{ "took" : 439, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 6, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "product", "_type" : "product", "_id" : "3", "_score" : 1.0, "_source" : { "id" : 3, "name" : "mobile phone C", "price" : "2600", "code" : "cmp", "color" : "black" } }, { "_index" : "product", "_type" : "product", "_id" : "5", "_score" : 1.0, "_source" : { "id" : 5, "name" : "mobile phone E", "price" : "2800", "code" : "emp", "color" : "silvery" } }, { "_index" : "product", "_type" : "product", "_id" : "4", "_score" : 1.0, "_source" : { "id" : 4, "name" : "mobile phone D", "price" : "2700", "code" : "dmp", "color" : "red" } }, { "_index" : "product", "_type" : "product", "_id" : "2", "_score" : 1.0, "_source" : { "id" : 2, "name" : "mobile phone B", "price" : "2200", "code" : "bmp", "color" : "white" } }, { "_index" : "product", "_type" : "product", "_id" : "6", "_score" : 1.0, "_source" : { "code" : "fmp", "color" : "white", "price" : "2750", "name" : "mobile phone F", "id" : 6 } }, { "_index" : "product", "_type" : "product", "_id" : "1", "_score" : 1.0, "_source" : { "id" : 1, "name" : "mobile phone A", "price" : "2000", "code" : "amp", "color" : "golden" } } ] } }
-
> Management > Dev Tools