扛得住的MySQL数据库架构

67 阅读5分钟

download:扛得住的MySQL数据库架构

如何获得MySQL最优性能?如何建立MySQL高可用集群?如何搭建稳定高效的MySQL环境?国内顶级电商公司数据库专家带你成为一名优秀的DBA。

适合人群

适合具有一定自学能力,掌握Linux系统基础知识及MySQL数据库基础知识的学员

    1. c add1(x *Number) *Number {

    2. e := new(Number)

    3. *e = x

    4. return e

    5. }

    6. func sub1(x *Number) *Number {

    7. return *x

    8. }

    9. func add(x, y *Number) *Number {

    10. if isZero(y) {

    11. return x

    12. }

    13. return add(add1(x), sub1(y))

    14. }

    15. func mul(x, y *Number) *Number {

    16. if isZero(x) || isZero(y) {

    17. return zero()

    18. }

    19. return add(mul(x, sub1(y)), x)

    20. }

    21. func fact(n *Number) *Number {

    22. if isZero(n) {

    23. return add1(zero())

    24. }

    25. return mul(fact(sub1(n)), n)

    26. }

    27. // -------------------------------------

    28. // Helpers to generate/count Peano integers

    29. func gen(n int) *Number {

    30. if n > 0 {

    31. return add1(gen(n - 1))

    32. }

    33. return zero()

    34. }

    35. func count(x *Number) int {

    36. if isZero(x) {

    37. return 0

    38. }

    39. return count(sub1(x)) + 1

    40. }

    41. // -------------------------------------

    42. // Print i! for i in [0,9]

    43. func main() {

    44. for i := 0; i <= 9; i++ {

    45. f := count(fact(gen(i)))

    46. fmt.Println(i, "! =", f)

    47. }

    48. }

    这是一个 n!的例子,但又不是单单的一个求 n! 的例子;这个例子让 e 们赶脚到了 —— 自然数能够结构出来或是逐个数出来!“皮亚诺自然数公理”:0 是一个自然数;每个自然数后面都跟有一个自然数;除 0 之外,每个自然数前面都有一个自然数;—— 0 很特殊,是第一个自然数,由于它前面没有自然数!同时例子中也反映了:阶乘依赖于自然数乘法;自然数乘法依赖于自然数加法,而自然数加法其实是个递归定义。

    自然数就是一个链表,链表的第一个元素代表 0 ,第二个元素代表 1,第三个 代表 2,不断下去。10 就是一个长度为 10 的链表!求 10!的思绪不再是循环一下,乘一下!而是,结构出一个长度是 10! 的链表,然后 count 一下长度,就是 10!多此一举?!这里一切的操作只树立在“加一”、“减一”和“递归”的根底之上。

  1. 并发求圆周率 π

    1. // Concurrent computation of pi.

    2. // See goo.gl/ZuTZM.

    3. //

    4. // This demonstrates Go's ability to handle

    5. // large numbers of concurrent processes.

    6. // It is an unreasonable way to calculate pi.

    7. package main

    8. import (

    9. "fmt"

    10. "math"

    11. )

    12. func main() {

    13. fmt.Println(pi(5000))

    14. }

    15. // pi launches n goroutines to compute an

    16. // approximation of pi.

    17. func pi(n int) float64 {

    18. ch := make(chan float64)

    19. for k := 0; k <= n; k++ {

    20. go term(ch, float64(k))

    21. }

    22. f := 0.0

    23. for k := 0; k <= n; k++ {

    24. f += <-ch

    25. }

    26. return f

    27. }

    28. func term(ch chan float64, k float64) {

    29. ch <- 4 * math.Pow(-1, k) / (2*k + 1)

    30. }

    割圆术求圆周长或是圆周率,听的比拟多,but,太不实在可行!不论公式怎样来的,总之有下面一个公式,本例子就是用这个公式求 π :

    例子中运用了并发求每单项的值,然后将每单项的值加起来,赶脚是,加的项数越多,便越接近与真值。

    go 中的管道(channel)提供了一种很便当的并发同步机制。管道是衔接多个并发函数的通道,就像是水管,一端能够流进去,另一端能够流进来。这里多个函数/进程将每个单项的求值放到管道之后,谁先谁后是不肯定的,然后另外一个函数/进程从管道中取值然后加起来,最后的结果便是就得的 π 的近似值。管道的同步操作曾经内置,也就是不需求偶们本人去管来的。 go 是偶目前看到的写并发程序最容易的言语,么有之一!

  2. 并发经过挑选法求素数

    1. // A concurrent prime sieve

    2. package main

    3. // Send the sequence 2, 3, 4, ... to channel 'ch'.

    4. func Generate(ch chan<- int) {

    5. for i := 2; ; i++ {

    6. ch <- i // Send 'i' to channel 'ch'.

    7. }

    8. }

    9. // Copy the values from channel 'in' to channel 'out',

    10. // removing those divisible by 'prime'.

    11. func Filter(in <-chan int, out chan<- int, prime int) {

    12. for {

    13. i := <-in // Receive value from 'in'.

    14. if i%prime != 0 {

    15. out <- i // Send 'i' to 'out'.

    16. }

    17. }

    18. }

    19. // The prime sieve: Daisy-chain Filter processes.

    20. func main() {

    21. ch := make(chan int) // Create a new channel.

    22. go Generate(ch) // Launch Generate goroutine.

    23. for i := 0; i < 10; i++ {

    24. prime := <-ch

    25. print(prime, "\n")

    26. ch1 := make(chan int)

    27. go Filter(ch, ch1, prime)

    28. ch = ch1

    29. }

    30. }

挑选法找素数是个不错的办法,2、3、4、5、6、7、8、9、10 ... 这些数中,第一个数 2 是素数,取出来,然后将 2 的倍数全部去掉;剩下的第一个数 3 还是素数,再去掉一切 3 的倍数,不断停止下去,就能找到很多素数。本例子也就是用的这个办法。

详细逻辑是:第一个管道记载从2开端的自然数,取第一自然数/质数 2;然后第二个管道记载从第一个管道中过滤后的一切自然数,再取一个质数 3;第三个管道取第二个管道中过滤后的数,又得一个质数;一切的管道都是无限长的,只需程序嚒有终止,这些挑选/过滤便不断在停止着。

  1. 孔明棋

    1. // This program solves the (English) peg

    2. // solitaire board game.

    3. // en.wikipedia.org/wiki/Peg_so…

    4. package main

    5. import "fmt"

    6. const N = 11 + 1 // length of a row (+1 for \n)

    7. // The board must be surrounded by 2 illegal

    8. // fields in each direction so that move()

    9. // doesn't need to check the board boundaries.

    10. // Periods represent illegal fields,

    11. // ● are pegs, and ○ are holes.

    12. var board = []rune(

    13. `...........

    14. ...........

    15. ....●●●....

    16. ....●●●....

    17. ..●●●●●●●..

    18. ..●●●○●●●..

    19. ..●●●●●●●..

    20. ....●●●....

    21. ....●●●....

    22. ...........

    23. ...........

    24. `)

    25. // center is the position of the center hole if

    26. // there is a single one; otherwise it is -1.

    27. var center int

    28. func init() {

    29. n := 0

    30. for pos, field := range board {

    31. if field == '○' {

    32. center = pos

    33. n++

    34. }