Finisky Garden

NLP, 软件工程, 产品设计

最近遇到个数据库表设计的问题,一张账号表,业务逻辑存在一列唯一accountId,是否还要在数据库表中加一列自增int作为主键?这应该是实践中普遍存在的问题,于是研究了下推荐做法。

简单回答:这个问题没有明确答案,看情况决定。stackoverflow的相关问题已经被标记为带有观点的回答。没有答案的原因在于是否需要自增主键主要取决于应用场景。不过我倾向于在绝大部分情况下,使用自增int主键,如有必要,增加额外的唯一键(如这里的accountId)进行索引和查询。

如下讨论都基于InnoDB引擎。

阅读全文 »

对一个DBA来说监控mysql服务器的性能指标是非常重要的。我们可以通过mysqlbench中的Server Status来轻松监测服务器最近一段时间的状态。但是这些指标具体代表了什么含义?比如,Selects per secondInnodb reads per second的区别是什么?如何评估服务器当前写入的性能?

下图展示了一个运行中的服务器性能状态: mysqlbench server status

阅读全文 »

Monitoring mysql server metrics is crucial for a DBA. Typically, we can simply monitor the recent server status summary through mysqlbench. But what's the meaning for these metrics? Some of them are self-explained such as connections and traffic while others are not. For example, what's the difference between Selects per second and Innodb reads per second? How to measure the write performance?

The following figure illustrates the serve status: mysqlbench server status

阅读全文 »

Master-slave replication is widely used in production. Monitoring the replication lag is a common and critical task. Typically, we are able to get the real-time difference between the master and the slave by periodically checking the Seconds_Behind_Master variable.

阅读全文 »

博客越写越多,将markdown源文件备份到github变得非常重要。github pages要求免费账户的username.github.io必须是public repository。网上主要有两种方案,一个是用hexo-git-backup插件,另一个是在username.github.io上创建另一个分支进行备份。

这两个方案都偏复杂,第一个引入了额外的依赖,而且看起来和使用的hexo版本还有点关系,第二个用一个branch进行备份略奇怪。

考虑到hexo-deployer-git使用的是.deploy_git目录,与源文件所在目录无关。那么最简单的方案是新开一个repository (public/private均可),让本地博客文件夹直接track这个新的repository即可。同时注意把不必要的文件和目录用.gitignore去除。

阅读全文 »

We developed a REST API long time ago and recently found that the released client has a flaw: the HttpRequestMessage is missing content-type application/json. In earlier version we manually deserialize the request json by reading the request body but now we leverage AspNetCore framework to automatically get the request structure from the API parameter. However, the legacy client will not work: HTTP 415 "Unsupported Media Type" error happens.

Therefore, for backward compatibility, we need to make the server treat all coming requests as application/json even it is a plain text (by default).

阅读全文 »

Someone may argue that there is no reason to disable ipv6 on Linux. For me, the reason is that in some ipv6-enabled website, I frequently be classified as a bot who need to solve captcha which is really annoying :-( . Consider the ipv6 address is never change (assigned by service provider), disable ipv6 is the simplest solution.

阅读全文 »

When we use entity framework to manipulate SQL database, we use where to query and include to load related entities (join operation).

Sample database table schema: Employee: Id, Name, Age Salary: Id, BasePay

Typical query scenarios:

  • Query employee 3's name => Employee.Where(x => x.Id == 3)
  • Query employee Jack's age => Employee.Where(x => x.Name == "Jack")
  • Query employee Jack's basepay => Employee.Where(x => x.Name == "Jack").Include(x => x.Salary) ...

To make the code clean and focus, the following examples will not include dbContext creation logic, we assume db = DbContext() which contains the Employee table.

For code reuse purpose, we may extract a common method like this:

Employee GetEntity(Func<Employee, bool> whereFunc, Func<DbSet<Employee>, IQueryable<Employee>> navigationFunc = null)
{
    IQueryable<Employee> query = navigationFunc != null ? navigationFunc(db.Employee) : db.Employee;
    var employee = await query.Where(x => whereFunc(x)).ToListAsync();
    return employee;
}

The implmentation looks simple and straightfoward: just extract the where and navigation Func<> to a function.

However, the above function has severe perfomance issue: when we execute the function, it retrieves all data from the table and locally applies where filter. Actually, the underlying mechanism is not that simple as entity framework will compile the expression and translated to SQL statements, though it looks like just a syntactic sugar.

阅读全文 »

Install an old library lightsquid on Ubuntu 20.04. When visit the cgi, internal server error pops up.

Debug the cgi by directly run it in /var/www/lightsquid:

/var/www/lightsquid$ perl index.cgi
Can't locate CGI.pm in @INC (you may need to install the CGI module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at index.cgi line 19.
BEGIN failed--compilation aborted at index.cgi line 19.

Seems that some modules are missing, after several search: https://packages.ubuntu.com/search?suite=trusty&arch=any&mode=filename&searchon=contents&keywords=cgi.pm

Combined with the error message:

$ apt install libcgi-pm-perl

Then it works:

/var/www/lightsquid$ perl index.cgi
```html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD>
  <META name="robots" content="noindex,nofollow">
  <META name="generator" content="LightSquid - http://lightsquid.sf.net">
  <META http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  <META HTTP-EQUIV="REFRESH" CONTENT="600">
...

BBR 是Google 2016年提出的TCP 拥塞控制算法,可以显著地提升Server的吞吐率并降低延迟。在VPS上实测效果非常明显,将VPS的下载速度由10Mbps左右提升至约50Mbps,自动视频质量也由240p升至1080p。

阅读全文 »
0%