Pengfei-xue.GitHub.io

programming your life!

Home ~/

View My GitHub Profile

  • mysql note

    alter table add MULTIPLE columns

    ALTER TABLE users
    ADD COLUMN `count` SMALLINT(6) NOT NULL AFTER `lastname`,
    ADD COLUMN `log` VARCHAR(12) NOT NULL AFTER `count`,
    ADD COLUMN `status` INT(10) UNSIGNED NOT NULL AFTER `log`;
    

    add columns for big table

    • Create a new table (using the structure of the current table) with the new column(s) included.
    • execute a INSERT INTO new_table (column1,..columnN) SELECT (column1,..columnN) FROM current_table;
    • rename the current table
    • rename the new table using the name of the current table.

    optimize mySql for faster alter table add column

    Clustered and Secondary Indexes

    Every InnoDB table has a special index called the clustered index where the data for the rows is stored. Typically, the clustered index is synonymous with the primary key. To get the best performance from queries, inserts, and other database operations, you must understand how InnoDB uses the clustered index to optimize the most common lookup and DML operations for each table.

    When you define a PRIMARY KEY on your table, InnoDB uses it as the clustered index. Define a primary key for each table that you create. If there is no logical unique and non-null column or set of columns, add a new auto-increment column, whose values are filled in automatically.

    If you do not define a PRIMARY KEY for your table, MySQL locates the first UNIQUE index where all the key columns are NOT NULL and InnoDB uses it as the clustered index.

    If the table has no PRIMARY KEY or suitable UNIQUE index, InnoDB internally generates a hidden clustered index on a synthetic column containing row ID values. The rows are ordered by the ID that InnoDB assigns to the rows in such a table. The row ID is a 6-byte field that increases monotonically as new rows are inserted. Thus, the rows ordered by the row ID are physically in insertion order.

    How the Clustered Index Speeds Up Queries

    Accessing a row through the clustered index is fast because the index search leads directly to the page with all the row data. If a table is large, the clustered index architecture often saves a disk I/O operation when compared to storage organizations that store row data using a different page from the index record. (For example, MyISAM uses one file for data rows and another for index records.)

    How Secondary Indexes Relate to the Clustered Index

    All indexes other than the clustered index are known as secondary indexes. In InnoDB, each record in a secondary index contains the primary key columns for the row, as well as the columns specified for the secondary index. InnoDB uses this primary key value to search for the row in the clustered index.

    If the primary key is long, the secondary indexes use more space, so it is advantageous to have a short primary key.

    Clustered and Secondary Indexes

    How to change the default charset of a MySQL table?

    ALTER TABLE etape_prospection CONVERT TO CHARACTER SET utf8;
    

    http://stackoverflow.com/questions/8906813/how-to-change-the-default-charset-of-a-mysql-table

    获取两个日期的天数差

    SELECT DATEDIFF('2010-10-08 18:23:13', '2010-09-21 21:40:36') AS days;
    
    +------+
    | days |
    +------+
    |   17 |
    +------+
    

    http://stackoverflow.com/questions/2546053/mysql-difference-between-two-timestamps-in-days

    在where条件中判断,日期是否在N天前/后

    delete from appchecker_checkrecord where check_time <= curdate() - interval 5 day;
    

    http://dba.stackexchange.com/questions/60837/datediff-in-mysql

  • linux network note

    second nameserver in /etc/resolv.conf not picked up by wget

    The default behavior for resolv.conf and the resolver is to try the servers in the order listed.

    The resolver will only try the next nameserver if the first nameserver times out. The resolv.conf manpage says:

    nameserver Name server IP address
    
    Internet address (in dot notation) of a name server that the resolver should query.
    Up to MAXNS (currently 3, see ) name servers may be listed, one per keyword.
    If there are multiple servers, the resolver library queries them in the order listed.
    

    And:

    (The algorithm used is to try a name server, and if the query times out, try the next, until out of name servers,
    then repeat trying all the name servers until a maximum number of retries are made.)
    Also see the resolver(5) manual page for more information.
    

    You can alter the resolver’s behavior using rotate, which will query the Nameservers in a round-robin order:

    rotate sets RES_ROTATE in res.options, which causes round robin selection of nameservers from among those listed.
    This has the effect of spreading the query load among all listed servers, rather than having all clients try the first listed server first every time.
    

    However, nslookup will use the second nameserver if it receives a SERVFAIL from the first nameserver. From the nslookup manpage:

    [no]fail Try the next nameserver if a nameserver responds with SERVFAIL or a referral (nofail) or terminate query (fail) on such a response.
    (Default = nofail)
    

    resolv.conf man page

    SSH access problem: debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY

    从ci ssh 到 backend.test 不成功 hang在 expecting SSH2_MSG_KEX_DH_GEX_REPLY

    ip li set mtu 1400 dev wlan0
    

    https://serverfault.com/questions/210408/ssh-access-problem-debug1-expecting-ssh2-msg-kex-dh-gex-reply/676026#676026

  • linux kernel note

    Check the open FD limit for a given process in Linux

    Count the entries in /proc//fd/ The hard and soft limits applying to the process can be found in `/proc//limits`

    ubunut upstart坑

    使用upstart 启动服务,ulimit没有正确设置,没有读取limits.conf 以及 ulimit 相关配置。

    http://bryanmarty.com/2012/02/10/setting-nofile-limit-upstart/ http://stackoverflow.com/questions/19995855/increase-max-open-files-for-ubuntu-upstart-initctl

  • golang note

    go by example

    https://gobyexample.com/
    

    Golang: format a string without printing?

    return fmt.Sprintf("at %v, %s", e.When, e.What)
    

    Golang: format a string without printing?

    How to find a type of a object in Golang?

    package main
    
    import (
        "fmt"
        "reflect"
    )
    
    func main() {
    
        tst := "string"
        tst2 := 10
        tst3 := 1.2
    
        fmt.Println(reflect.TypeOf(tst))
        fmt.Println(reflect.TypeOf(tst2))
        fmt.Println(reflect.TypeOf(tst3))
    
    }
    
    // 或者
    func typeof(v interface{}) string {
        return fmt.Sprintf("%T", v)
    }
    
    // 或者
    func typeof(v interface{}) string {
        return reflect.TypeOf(v).String()
    }
    
    // 或者
    func typeof(v interface{}) string {
        switch t := v.(type) {
        case int:
            return "int"
        case float64:
            return "float64"
        //... etc
        default:
            _ = t
            return "unknown"
        }
    }
    

    How to find a type of a object in Golang?

  • git note

    How to .gitignore all files/folder in a folder, but not the folder itself?

    You can’t commit empty folders in git. If you want it to show up, you need to put something in it, even just an empty file.

    For example, add an empty file called .gitkeep to the folder you want to keep, then in your .gitignore file write:

    # exclude everything
    somefolder/*
    
    # exception to the rule
    !somefolder/.gitkeep