Synchronize contract data to database classic case 3

In the previous article, we solved the problem of no listing time in the market table by analyzing the activity table and creating the meta_view_create_latest view.

But for the market table, we seem to have forgotten something. Yes, each time the data is obtained from the contract and saved, although the latest and most complete product list is guaranteed, we have not deleted the "cancelled" and "sold" product information from the market table. This is obviously unreasonable.

Let's solve this problem below.

1. Find out when "cancelled" and "closed"

In order to find out the latest "cancelled" time and "completed" time, we create 2 new views respectively according to the method introduced above: meta_view_cancel_latest, meta_view_confirm_latest

2. Compare the time points to find out the data that should be deleted from the market table

Compare the cancellation time and creation time. If the cancellation time is later than the creation time, it means that this product information should be deleted from the market table. The following statement is used to create a new view "meta_view_must_del_cancel"

```sql

SELECT c.contract,c.tokenID FROM (SELECT a.`contract`,a.`tokenID`,IF(a.`timestamp`>b.`timestamp`,1,0) must_del FROM meta_view_cancel_latest a LEFT JOIN meta_view_create_latest b ON a.`contract`=b.`contract` AND a.`tokenID`=b.`tokenID`) c WHERE c.must_del=1;

```

Compare the delivery time and creation time. If the transaction time is later than the creation time, it means that this product information should be deleted from the market table. The following statement is used to create a new view "meta_view_must_del_confirm"

```sql

SELECT a.`contract`,a.`tokenID`,IF(a.`timestamp`>b.`timestamp`,1,0) must_del FROM meta_view_confirm_latest a LEFT JOIN meta_view_create_latest b ON a.`contract`=b.`contract` AND a.`tokenID`=b.`tokenID` HAVING must_del=1;

```

Above we used two methods to filter data, interested students can test and compare the differences between the two methods

3. Data volume detection

Next, we try to query the tables to see the comparison of the number of product information that should be deleted due to "cancellation" in the market table.

```sql

SELECT * FROM meta_view_must_del_cancel a LEFT JOIN meta_market b ON a.`contract`=b.`collection` AND a.`tokenID`=b.`tokenID`;

```

We found that there are a lot of NULL data. That is, more information should be deleted than in the market table. This shows that when we obtain the list data of the listed products from the contract, some products have already been cancelled. The running time of our program for synchronizing data is later than the actual running time of the contract. This is a normal phenomenon, you only need to filter out the data when deleting.

Similarly, the query method for redundant data in the market table due to "completed transactions" is as follows:

```sql

SELECT * FROM meta_view_must_del_confirm a LEFT JOIN meta_market_copy b ON a.`contract`=b.`collection` AND a.`tokenID`=b.`tokenID`;

```

4. Data deletion

Finally, we need to clean up the data that should not be in the market list due to "unlisted" and "sold".

```sql

DELETE b.* FROM meta_view_must_del_cancel a LEFT JOIN meta_market_copy b ON a.`contract`=b.`collection` AND a.`tokenID`=b.`tokenID` WHERE collection IS NOT NULL;

DELETE b.* FROM meta_view_must_del_confirm a LEFT JOIN meta_market_copy b ON a.`contract`=b.`collection` AND a.`tokenID`=b.`tokenID` WHERE collection IS NOT NULL;

```

Guess you like

Origin blog.csdn.net/2301_76642277/article/details/130148462