简体   繁体   English

MySQL按国家/地区登录日志,最佳方法

[英]MySQL click log by country, best approach

I'm planning to log referral clicks in mysql by the country of the IP. 我打算按IP国家记录mysql中的引荐点击。 Let's say my table is named referral_clicks and has the columns id , referral_id , country . 假设我的表格命名为referral_clicks并具有列idreferral_idcountry I have 2 approaches in mind now: 我现在有两种思路:

  1. Create another column clicks which is set to +1 for every country / referral_id. 创建另一个针对每个国家/ referral_id设置为+1的列clicks This means that I would have to check first if the row for the specific referral_id and country already exists and if not, create it. 这意味着我将必须首先检查特定referral_id和国家/地区的行是否已经存在,如果不存在,请创建它。

  2. Insert a new row for every request. 为每个请求插入一个新行。 My concern here is, that the table might geht messy and too big, as might get very much referral requests. 我在这里担心的是,该表可能会变得凌乱且太大,因为可能会收到很多引用请求。

What would be the best approach now for something like that, or is there evern a better approach? 对于这样的事情,现在最好的方法是什么,或者有没有更好的方法?

Use INSERT ... ON DUPLICATE KEY UPDATE . 使用INSERT ... ON DUPLICATE KEY UPDATE

I suggest you create a table with the following columns. 我建议您使用以下各列创建一个表。

id                (autoincrement)
referral_id
country
clickdate
clicks

I suggest you create a unique index of (referral_id,country,clickdate) . 我建议您创建(referral_id,country,clickdate)的唯一索引。

Then, I suggest you use the following SQL each time you want to log a click: 然后,建议您每次想要记录一次单击时都使用以下SQL:

INSERT INTO referral_clicks (referral_id, country, clickdate, clicks) 
                     VALUES ('whatever', 'whatcountry', CURDATE(), 1) 
ON DUPLICATE KEY UPDATE clicks=click+1

This will start a new row for each referral id, for each country, for each date. 这将为每个推荐人ID,每个国家/地区和每个日期开始一个新行。 If the row already exists it will increment clicks for you. 如果该行已经存在,它将为您增加clicks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM