5

We are having a Analytics product. For each of our customer we give one JavaScript code, they put that in their web sites. If a user visit our customer site the java script code hit our server so that we store this page visit on behalf of this customer. Each customer contains unique domain name.

we are storing this page visits in MySql table.

Following is the table schema.

CREATE TABLE `page_visits` (
  `domain` varchar(50) DEFAULT NULL,
  `guid` varchar(100) DEFAULT NULL,
  `sid` varchar(100) DEFAULT NULL,
  `url` varchar(2500) DEFAULT NULL,
  `ip` varchar(20) DEFAULT NULL,
  `is_new` varchar(20) DEFAULT NULL,
  `ref` varchar(2500) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `stats_time` datetime DEFAULT NULL,
  `country` varchar(50) DEFAULT NULL,
  `region` varchar(50) DEFAULT NULL,
  `city` varchar(50) DEFAULT NULL,
  `city_lat_long` varchar(50) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  KEY `sid_index` (`sid`) USING BTREE,
  KEY `domain_index` (`domain`),
  KEY `email_index` (`email`),
  KEY `stats_time_index` (`stats_time`),
  KEY `domain_statstime` (`domain`,`stats_time`),
  KEY `domain_email` (`domain`,`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

We don't have primary key for this table.

MySql server details

It is Google cloud MySql (version is 5.6) and storage capacity is 10TB.

As of now we are having 350 million rows in our table and table size is 300 GB. We are storing all of our customer details in the same table even though there is no relation between one customer to another.

Problem 1: For few of our customers having huge number of rows in table, so performance of queries against these customers are very slow.

Example Query 1:

SELECT count(DISTINCT sid) AS count,count(sid) AS total FROM page_views WHERE domain = 'aaa' AND stats_time BETWEEN CONVERT_TZ('2015-02-05 00:00:00','+05:30','+00:00') AND CONVERT_TZ('2016-01-01 23:59:59','+05:30','+00:00');
+---------+---------+
| count   | total   |
+---------+---------+
| 1056546 | 2713729 |
+---------+---------+
1 row in set (13 min 19.71 sec)

I will update more queries here. We need results in below 5-10 seconds, will it be possible?

Problem 2: The table size is rapidly increasing, we might hit table size 5 TB by this year end so we want to shard our table. We want to keep all records related to one customer in one machine. What are the best practises for this sharding.

We are thinking following approaches for above issues, please suggest us best practices to overcome these issues.

Create separate table for each customer

1) What are the advantages and disadvantages if we create separate table for each customer. As of now we are having 30k customers we might hit 100k by this year end that means 100k tables in DB. We access all tables simultaneously for Read and Write.

2) We will go with same table and will create partitions based on date range

UPDATE : Is a "customer" determined by the domain? Answer is Yes

Thanks

2 Answers 2

5

First, a critique if the excessively large datatypes:

  `domain` varchar(50) DEFAULT NULL,  -- normalize to MEDIUMINT UNSIGNED (3 bytes)
  `guid` varchar(100) DEFAULT NULL,  -- what is this for?
  `sid` varchar(100) DEFAULT NULL,  -- varchar?
  `url` varchar(2500) DEFAULT NULL,
  `ip` varchar(20) DEFAULT NULL,  -- too big for IPv4, too small for IPv6; see below
  `is_new` varchar(20) DEFAULT NULL,  -- flag?  Consider `TINYINT` or `ENUM`
  `ref` varchar(2500) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,  -- normalize! (add new rows as new agents are created)
  `stats_time` datetime DEFAULT NULL,
  `country` varchar(50) DEFAULT NULL,  -- use standard 2-letter code (see below)
  `region` varchar(50) DEFAULT NULL,  -- see below
  `city` varchar(50) DEFAULT NULL,  -- see below
  `city_lat_long` varchar(50) DEFAULT NULL,  -- unusable in current format; toss?
  `email` varchar(100) DEFAULT NULL,

For IP addresses, use inet6_aton(), then store in BINARY(16).

For country, use CHAR(2) CHARACTER SET ascii -- only 2 bytes.

country + region + city + (maybe) latlng -- normalize this to a "location".

All these changes may cut the disk footprint in half. Smaller --> more cacheable --> less I/O --> faster.

Other issues...

To greatly speed up your sid counter, change

KEY `domain_statstime` (`domain`,`stats_time`),

to

KEY dss (domain_id,`stats_time`, sid),

That will be a "covering index", hence won't have to bounce between the index and the data 2713729 times -- the bouncing is what cost 13 minutes. (domain_id is discussed below.)

This is redundant with the above index, DROP it: KEY domain_index (domain)

Is a "customer" determined by the domain?

Every InnoDB table must have a PRIMARY KEY. There are 3 ways to get a PK; you picked the 'worst' one -- a hidden 6-byte integer fabricated by the engine. I assume there is no 'natural' PK available from some combination of columns? Then, an explicit BIGINT UNSIGNED is called for. (Yes that would be 8 bytes, but various forms of maintenance need an explicit PK.)

If most queries include WHERE domain = '...', then I recommend the following. (And this will greatly improve all such queries.)

id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
domain_id MEDIUMINT UNSIGNED NOT NULL,   -- normalized to `Domains`
PRIMARY KEY(domain_id, id),  -- clustering on customer gives you the speedup
INDEX(id)  -- this keeps AUTO_INCREMENT happy

Recommend you look into pt-online-schema-change for making all these changes. However, I don't know if it can work without an explicit PRIMARY KEY.

"Separate table for each customer"? No. This is a common question; the resounding answer is No. I won't repeat all the reasons for not having 100K tables.

Sharding

"Sharding" is splitting the data across multiple machines.

To do sharding, you need to have code somewhere that looks at domain and decides which server will handle the query, then hands it off. Sharding is advisable when you have write scaling problems. You did not mention such, so it is unclear whether sharding is advisable.

When sharding on something like domain (or domain_id), you could use (1) a hash to pick the server, (2) a dictionary lookup (of 100K rows), or (3) a hybrid.

I like the hybrid -- hash to, say, 1024 values, then look up into a 1024-row table to see which machine has the data. Since adding a new shard and migrating a user to a different shard are major undertakings, I feel that the hybrid is a reasonable compromise. The lookup table needs to be distributed to all clients that redirect actions to shards.

If your 'writing' is running out of steam, see high speed ingestion for possible ways to speed that up.

PARTITIONing

PARTITIONing is splitting the data across multiple "sub-tables".

There are only a limited number of use cases where partitioning buys you any performance. You not indicated that any apply to your use case. Read that blog and see if you think that partitioning might be useful.

You mentioned "partition by date range". Will most of the queries include a date range? If so, such partitioning may be advisable. (See the link above for best practices.) Some other options come to mind:

Plan A: PRIMARY KEY(domain_id, stats_time, id) But that is bulky and requires even more overhead on each secondary index. (Each secondary index silently includes all the columns of the PK.)

Plan B: Have stats_time include microseconds, then tweak the values to avoid having dups. Then use stats_time instead of id. But this requires some added complexity, especially if there are multiple clients inserting data. (I can elaborate if needed.)

Plan C: Have a table that maps stats_time values to ids. Look up the id range before doing the real query, then use both WHERE id BETWEEN ... AND stats_time .... (Again, messy code.)

Summary tables

Are many of the queries of the form of counting things over date ranges? Suggest having Summary Tables based perhaps on per-hour. More discussion.

COUNT(DISTINCT sid) is especially difficult to fold into summary tables. For example, the unique counts for each hour cannot be added together to get the unique count for the day. But I have a technique for that, too.

8
  • @James Thank you for the detailed explanation, could you share any links of explanation why 100k tables is not a good decision.
    – Rams
    Jan 12, 2016 at 6:48
  • The 100K tables is a common question on this forum. Here are some of the arguments against it: OS overhead and slowdown; complexity in your code; very little advantage. There would probably be lots of tiny tables and a few huge tables -- each extreme has its own inefficiencies.
    – Rick James
    Jan 12, 2016 at 19:00
  • @cloudpre - Thanks for the comment. It's the type of stuff I love talking about.
    – Rick James
    Jan 12, 2016 at 19:02
  • @RickJames I will do schema corrections and optimize queries with proper indexes as per your suggestions. Just assume that even after all these changes suppose if we have huge index file that can't fit into memory in that case do we need shard our table?? or just range partition does the work ??. We have 10 storage capacity in instance.
    – Rams
    Jan 13, 2016 at 13:40
  • Spinning drives? Or SSDs? What do you mean by "10 storage capacity"?
    – Rick James
    Jan 13, 2016 at 18:43
0

I wouldn't do this if i were you. First thing that come to mind would be, on receive a pageview message, i send the message to a queue so that a worker can pickup and insert to database later (in bulk maybe); also i increase the counter of siteid:date in redis (for example). Doing count in sql is just a bad idea for this scenario.

6
  • @Tran hey thank for your answer, do you anyway to do it on MySql
    – Rams
    Jan 11, 2016 at 9:39
  • @Rams you want to do it anyway in sql? Jan 11, 2016 at 9:40
  • @Tran we want to do it on any solution which available on cloud like Google cloud sql.
    – Rams
    Jan 11, 2016 at 9:43
  • @Rams well rabbitmq and redis are available on google cloud. Jan 11, 2016 at 9:44
  • @Tran ok I will look into this. Any suggestion on sql ??
    – Rams
    Jan 11, 2016 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.