SQLAlchemy 小计2 (索引)

针对PostgreSQL

  1. 函数索引
    class test_table(db.Model):
            username = db.Column(String(20))
    
            __table_args_ = (Index('idx_username', func.lower('username'), unique=True), )
            # 生成SQL如下:
            # create unique index idx_username on test_table  lower(username)
    
  2. 指定索引类型
    __table_args__ = (Index('idx_point', 'point', postgresql_using='gist'), )
    # 生成SQL:
    # create index idx_point on table_name using(point)
    
  3. 条件索引
    __table_args__ = (Index('idx_where', 'column', postgresql_where = model.c.column > 10), )
    # 生成SQL:
    # create index idx_where on table_name (column) where column > 10
    

SQLAlchmey 创建复合索引

关键字:__table_args__

class User(db.Model):
    """
    user model
    """
    __tablename__ = 'users'

    id = db.Column(BigInteger, primary_key=True)
    username = db.Column(String(30), nullable=False)
    password = db.Column(db.String(60), nullable=False)
    reg_dateline = db.Column(db.String(12), nullable=False)

    # table_args
        # 创建名为 idx_test 含字段 username和reg_dateline的 索引
    __table_args__ = ( Index('idx_test','username','reg_dateline') )

电商SKU之Python

itertools是Python内置标准库之一,itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用。在BigData分析处理上也会经常使用。

简单的笛卡尔乘积问题

import itertools

# 商品属性字典
attributes = [
    # 尺码属性
    ['S','M','L','XL','XXL'],
    # 颜色属性
    ['黑色','白色','灰色','天蓝黑'],
    # 季节属性
    ['春','夏','秋','冬']
]

skus = []
for i in itertools.product(*attributes):    
    skus.append(i)
print(skus)

输出结果:

[(‘S’, ‘黑色’, ‘春’), (‘S’, ‘黑色’, ‘夏’), (‘S’, ‘黑色’, ‘秋’), (‘S’, ‘黑色’, ‘冬’), (‘S’, ‘白色’, ‘春’), (‘S’, ‘白色’, ‘夏’), (‘S’, ‘白色’, ‘秋’), (‘S’, ‘白色’, ‘冬’), (‘S’, ‘灰色’, ‘春’), (‘S’, ‘灰色’, ‘夏’), (‘S’, ‘灰色’, ‘秋’), (‘S’, ‘ 灰色’, ‘冬’), (‘S’, ‘天蓝黑’, ‘春’), (‘S’, ‘天蓝黑’, ‘夏’), (‘S’, ‘天蓝黑’, ‘秋’), (‘S’, ‘天蓝黑’, ‘冬’), (‘M’, ‘黑色’, ‘春’), (‘M’, ‘黑色’, ‘夏’), (‘M’, ‘黑色’, ‘秋’), (‘M’, ‘黑色’, ‘冬’), (‘M’, ‘白色’, ‘春’), (‘M’, ‘白色’, ‘夏’), (‘M’, ‘ 白色’, ‘秋’), (‘M’, ‘白色’, ‘冬’), (‘M’, ‘灰色’, ‘春’), (‘M’, ‘灰色’, ‘夏’), (‘M’, ‘灰色’, ‘秋’), (‘M’, ‘灰色’, ‘冬’), (‘M’, ‘天蓝黑’, ‘春’), (‘M’, ‘天蓝黑’, ‘夏’), (‘M’, ‘天蓝黑’, ‘秋’), (‘M’, ‘天蓝黑’, ‘冬’), (‘L’, ‘黑色’, ‘春’), (‘L’, ‘ 黑色’, ‘夏’), (‘L’, ‘黑色’, ‘秋’), (‘L’, ‘黑色’, ‘冬’), (‘L’, ‘白色’, ‘春’), (‘L’, ‘白色’, ‘夏’), (‘L’, ‘白色’, ‘秋’), (‘L’, ‘白色’, ‘冬’), (‘L’, ‘灰色’, ‘春’), (‘L’, ‘灰色’, ‘夏’), (‘L’, ‘灰色’, ‘秋’), (‘L’, ‘灰色’, ‘冬’), (‘L’, ‘天蓝黑’, ‘春’), (‘L’, ‘天蓝黑’, ‘夏’), (‘L’, ‘天蓝黑’, ‘秋’), (‘L’, ‘天蓝黑’, ‘冬’), (‘XL’, ‘黑色’, ‘春’), (‘XL’, ‘黑色’, ‘夏’), (‘XL’, ‘黑色’, ‘秋’), (‘XL’, ‘黑色’, ‘冬’), (‘XL’, ‘白色’, ‘春’), (‘XL’, ‘白色’, ‘夏’), (‘XL’, ‘白色’, ‘秋’), (‘XL’, ‘白色’, ‘冬’), (‘XL’, ‘灰色’, ‘春’), (‘XL’, ‘灰色’, ‘夏’), (‘XL’, ‘灰色’, ‘秋’), (‘XL’, ‘灰色’, ‘冬’), (‘XL’, ‘天蓝黑’, ‘春’), (‘XL’, ‘天蓝黑’, ‘夏’), (‘XL’, ‘天蓝黑’, ‘秋’), (‘XL’, ‘天蓝黑’, ‘冬’), (‘XXL’, ‘黑色’, ‘春’), (‘XXL’, ‘黑色’, ‘夏’), (‘XXL’, ‘黑色’, ‘秋’), (‘XXL’, ‘黑色’, ‘冬’), (‘XXL’, ‘白色’, ‘春’), (‘XXL’, ‘白色’, ‘夏’), (‘XXL’, ‘白色’, ‘秋’), (‘XXL’, ‘白色’, ‘冬’), (‘XXL’, ‘灰色’, ‘春’), (‘XXL’, ‘灰色’, ‘夏’), (‘XXL’, ‘灰色’, ‘秋’), (‘XXL’, ‘灰色’, ‘冬’), (‘XXL’, ‘天蓝黑’, ‘春’), (‘XXL’, ‘天蓝黑’, ‘夏’), (‘XXL’, ‘天蓝黑’, ‘秋’), (‘XXL’, ‘天蓝黑’, ‘冬’)]