西维蜀黍

【JavaEE】Java Servlet和 JSP(JavaServer Pages)

狭义的Servlet是指Java语言中的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。

  ...


【Database】索引 - 聚集索引(Clustered Index)与非聚集索引(Non-clustered Index)

聚集索引(Clustered Index)非聚集索引(Non-clustered Index)

聚集索引(Clustered Index) - storing all row data within the index)

Clustering alters the data block into a certain distinct order to match the index, resulting in the row data being stored in order. Therefore, only one clustered index can be created on a given database table. Clustered indices can greatly increase overall speed of retrieval, but usually only where the data is accessed sequentially in the same or reverse order of the clustered index, or when a range of items is selected.

Since the physical records are in this sort order on disk, the next row item in the sequence is immediately before or after the last one, and so fewer data block reads are required. The primary feature of a clustered index is therefore the ordering of the physical data rows in accordance with the index blocks that point to them. Some databases separate the data and index blocks into separate files, others put two completely different data blocks within the same physical file(s).

非聚集索引(Non-clustered Index)- storing only references to the data within the index

The data is present in arbitrary order, but the logical ordering is specified by the index. The data rows may be spread throughout the table regardless of the value of the indexed column or expression. The non-clustered index tree contains the index keys in sorted order, with the leaf level of the index containing the pointer to the record (page and the row number in the data page in page-organized engines; row offset in file-organized engines).

In a non-clustered index,

  • The physical order of the rows is not the same as the index order.
  • The indexed columns are typically non-primary key columns used in JOIN, WHERE, and ORDER BY clauses.

There can be more than one non-clustered index on a database table.

索引数据的存储方式 - 聚集索引(聚簇索引,Clustered Index)

聚集索引(clustered index),也称为聚簇索引

聚簇索引并不是一种单独的索引类型,而是一种索引数据的存储方式

当表有聚簇索引时,它的行数据实际上存储在索引的叶子页(leaf page)中。因为无法同时把数据行存放在两个不同的地方,所以一张表只能有一个聚簇索引

  ...


【Django】通过 ORM 访问 MySQL,插入 Emoji 报错

Problem

>>> paras = {u'name': u'\U0001f600'}
>>> from core import models
>>> models.Channel.objects.create(**paras)
Traceback (most recent call last):
    ...
  File "/Library/Python/2.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
InternalError: (1366, u"Incorrect string value: '\\xF0\\x9F\\x98\\x80' for column 'name' at row 1")

通过 ORM插入一条数据,在其中的一个类型为varchar的字段中插入一个 Emoji 表情,我插的是😀(对应的字符串为u'\U0001f600'),插入后:提示以下错误:

{InternalError}(1366, u"Incorrect string value: '\\xF0\\x9F\\x98\\x80' for column 'name' at row 1")
  ...


【MySQL】MySQL 的存储引擎(Storage Engines)- MyISAM 与 InnoDB

MySQL 的存储引擎(Storage Engines)

MySQL 有两种存储引擎(Storage Engines):

  • InnoDB’s
  • MyISAM

InnoDB is a general-purpose storage engine that balances high reliability and high performance. In MySQL 8.0, InnoDB is the default MySQL storage engine. Unless you have configured a different default storage engine, issuing a CREATE TABLE statement without an ENGINE clause creates an InnoDB table.

MyISAM

MyISAM不支持聚簇索引,因此,MyISAM中的主键索引(Primary Index) 和辅助索引(Secondary Index),都是非聚集索引(Non-clustered Index)

在MyISAM的B+Tree的叶子节点上的data域中,并不是保存数据本身,而是保存数据存放在磁盘中的地址。

  ...


【Django】Django 项目部署到 uWSGI + Nginx 作为生产环境

Workflow

当客户端发出一个 HTTP 请求后,是如何转到我们的应用程序处理并返回的呢?

我根据其架构组成的不同将这个过程的实现分为两种情况:

两级结构

在两级结构中:

  • uWSGI可以作为WSGI Server(WSGI 服务器),它基于了 HTTP 协议、 WSGI(Web Server Gateway Interface) 协议和 usgi 协议;
  • Flask应用可以作为 WSGI Application,基于 WSGI 协议实现。

当有客户端发来请求,uWSGI接受请求,调用 WSGI Application (或者说Flask应用)。当WSGI Application处理完成后,将结果返回给uWSGI,uWSGI构造相应的 HTTP Response,最终将结果返回给客户端。

通常来说,Flask等Web框架会自己附带一个 WSGI 服务器(这就是 Flask 应用可以直接启动的原因),但是这只是在开发阶段被用到而言。在生产环境中,仍然需要将 Flask 部署到一个高性能的 WSGI Server 中,比如 uWSGI,alternative 还有 gunicorn (Green Unicorn), wsgiref。

三级结构

在三级结构中:

  • uWSGI Server 作为中间件,它基于WSGI 协议与Nginx通信,同时与实现了 WSGI 协议的 WSGI Application(比如一个 Flask Application)进行通信

当有客户端发来请求,Nginx(Web Server)先做处理静态资源(静态资源是Nginx的强项),无法处理的动态请求以 WSGI 协议的格式转发给 WSGI Application(比如一个 Flask Application)。

这里,我们多了一层反向代理有什么好处?

  • 提高Web server性能(uWSGI处理静态资源不如nginx)nginx会在收到一个完整的http请求后再转发给wWSGI)
  • Nginx可以做负载均衡(客户端是和Nginx交互而不是uWSGI),以提升高可用
  ...