自检系列

本文基于 ruby-trivia 和
ruby-style完毕,节选部分难点,并更进一竿研商原理,用以抓好本人对ruby基础的了然和应用。

Q: Is Ruby a statically typed or a dynamically typed language?A:
Dynamically typed since type checking is done at runtime.

MySQL 5.7 Reference
Manual  /  …  /  Comparison
of B-Tree and Hash Indexes

Q: Is Ruby a strongly typed or a weakly typed language?A: Strongly typed
since an object’s type is checked before an operation is performed on
it.

9.3.8 Comparison of B-Tree and Hash Indexes

Understanding the B-tree and hash data structures can help predict how
different queries perform on different storage engines that use these
data structures in their indexes, particularly for the MEMORY storage
engine that lets you choose B-tree or hash indexes.

Q: What is the difference between a statement and an expression in
Ruby?A: All statements are expressions in Ruby since all statements
return a value.(expressions > statements)

B-Tree Index Characteristics

A B-tree index can be used for column comparisons in expressions that
use
the =>>=<<=,
or BETWEEN operators.
The index also can be used
for LIKEcomparisons
if the argument
to LIKE is
a constant string that does not start with a wildcard character. For
example, the
following SELECT statements
use indexes:

SELECT * FROM tbl_name WHERE key_col LIKE 'Patrick%';
SELECT * FROM tbl_name WHERE key_col LIKE 'Pat%_ck%';

In the first statement, only rows with 'Patrick' <= key_col <
‘Patricl’ are considered. In the second statement, only rows
with 'Pat' <=key_col < ‘Pau’ are considered.

The
following SELECT statements
do not use indexes:

SELECT * FROM tbl_name WHERE key_col LIKE '%Patrick%';
SELECT * FROM tbl_name WHERE key_col LIKE other_col;

In the first statement,
the LIKE value
begins with a wildcard character. In the second statement,
the LIKE value
is not a constant.

If you use ... LIKE '%string%’ and string is longer than three
characters, MySQL uses the Turbo Boyer-Moore
algorithm to initialize the pattern for the string and then uses this
pattern to perform the search more quickly.

A search using col_name IS NULL employs indexes if col_name is
indexed.

Any index that does not span
all AND levels
in the WHERE clause is not used to optimize the query. In other words,
to be able to use an index, a prefix of the index must be used in
every AND group.

The following WHERE clauses use indexes:

... WHERE index_part1=1 AND index_part2=2 AND other_column=3

    /* index = 1 OR index = 2 */
... WHERE index=1 OR A=10 AND index=2

    /* optimized like "index_part1='hello'" */
... WHERE index_part1='hello' AND index_part3=5

    /* Can use index on index1 but not on index2 or index3 */
... WHERE index1=1 AND index2=2 OR index1=3 AND index3=3;

These WHERE clauses do not use
indexes:

    /* index_part1 is not used */
... WHERE index_part2=1 AND index_part3=2

    /*  Index is not used in both parts of the WHERE clause  */
... WHERE index=1 OR A=10

    /* No index spans all rows  */
... WHERE index_part1=1 OR index_part2=10

Sometimes MySQL does not use an index, even if one is available. One
circumstance under which this occurs is when the optimizer estimates
that using the index would require MySQL to access a very large
percentage of the rows in the table. (In this case, a table scan is
likely to be much faster because it requires fewer seeks.) However, if
such a query uses LIMIT to retrieve only some of the rows, MySQL uses
an index anyway, because it can much more quickly find the few rows to
return in the result.

 

 

 

app

0-差不离须要扫描全表的查询,不选取索引,不限类型,此时轻重颠倒,除非含limit;

1-B-Tree索引帮衬 =>>=<<=,
BETWEEN,首字符不为通配符的LIKE;

 

 

 

 

Q: Does String include the Enumerable module?A: No.

9.3.1 How MySQL Uses Indexes

Indexes are used to find rows with specific column values quickly.
Without an index, MySQL must begin with the first row and then read
through the entire table to find the relevant rows. The larger the
table, the more this costs. If the table has an index for the columns in
question, MySQL can quickly determine the position to seek to in the
middle of the data file without having to look at all the data. This is
much faster than reading every row sequentially.

Most MySQL indexes (PRIMARY KEYUNIQUEINDEX, and FULLTEXT)
are stored
in B-trees.
Exceptions: Indexes on spatial data types use R-trees; MEMORY tables
also support hash
indexes; InnoDB uses
inverted lists for FULLTEXT indexes.

In general, indexes are used as described in the following discussion.
Characteristics specific to hash indexes (as used inMEMORY tables) are
described in Section 9.3.8, “Comparison of B-Tree and Hash
Indexes”.

MySQL uses indexes for these operations:

  • To find the rows matching a WHERE clause quickly.

  • To eliminate rows from consideration. If there is a choice between
    multiple indexes, MySQL normally uses the index that finds the
    smallest number of rows (the
    most selective index).

  • If the table has a multiple-column index, any leftmost prefix of the
    index can be used by the optimizer to look up rows. For example, if
    you have a three-column index on (col1, col2, col3), you have
    indexed search capabilities on (col1)(col1, col2),
    and (col1, col2, col3). For more information, see Section 9.3.5,
    “Multiple-Column
    Indexes”.

  • To retrieve rows from other tables when performing joins. MySQL can
    use indexes on columns more efficiently if they are declared as the
    same type and size. In this
    context, VARCHAR and CHAR are
    considered the same if they are declared as the same size. For
    example, VARCHAR(10) and CHAR(10) are the same size,
    but VARCHAR(10) andCHAR(15) are not.

    For comparisons between nonbinary string columns, both columns
    should use the same character set. For example, comparing
    utf8 column with a latin1 column precludes use of an index.

    Comparison of dissimilar columns (comparing a string column to a
    temporal or numeric column, for example) may prevent use of indexes
    if values cannot be compared directly without conversion. For a
    given value such as 1 in the numeric column, it might compare
    equal to any number of values in the string column such
    as '1'' 1''00001', or '01.e1'. This rules out use of any
    indexes for the string column.

  • To find
    the MIN() or MAX() value
    for a specific indexed column key_col. This is optimized by a
    preprocessor that checks whether you are
    using WHERE key_part_N = constant on all key parts that occur
    before key_col in the index. In this case, MySQL does a single
    key lookup for
    each MIN() or MAX() expression
    and replaces it with a constant. If all expressions are replaced
    with constants, the query returns at once. For example:

    SELECT MIN(key_part2),MAX(key_part2)
      FROM tbl_name WHERE key_part1=10;
    
  • To sort or group a table if the sorting or grouping is done on a
    leftmost prefix of a usable index (for
    example, ORDER BY key_part1key_part2). If all key parts are
    followed by DESC, the key is read in reverse order.
    SeeSection 9.2.1.13, “ORDER BY
    Optimization”,
    and Section 9.2.1.14, “GROUP BY
    Optimization”.

  • In some cases, a query can be optimized to retrieve values without
    consulting the data rows. (An index that provides all the necessary
    results for a query is called a covering
    index.)
    If a query uses from a table only columns that are included in some
    index, the selected values can be retrieved from the index tree for
    greater speed:

    SELECT key_part3 FROM tbl_name
      WHERE key_part1=1
    

Indexes are less important for queries on small tables, or big tables
where report queries process most or all of the rows. When a query needs
to access most of the rows, reading sequentially is faster than working
through an index. Sequential reads minimize disk seeks, even if not all
the rows are needed for the query. See Section 9.2.1.19, “Avoiding Full
Table
Scans” for
details.

 

app

0-无index索引需全表查询。

Q: What method might you use to enumerate over a string?A:
String#each_char

Q: How might you specify a default value for a hash?A: Pass the default
values as arguments to ::new on initialization or change the default
directly with the method Hash#default. You may also provide a default
at the time of query with Hash#fetch.C:
在少数情形下使用Hash#fetch是相比较确切的:

  • 当管理相应留存的哈希键时,使用 Hash#fetch

heroes = { batman: 'Bruce Wayne', superman: 'Clark Kent' }# 差 - 如果我们打错了哈希键,则难以发现这个错误heroes[:batman] # => 'Bruce Wayne'heroes[:supermann] # => nil# 好 - fetch 会抛出 KeyError 使这个错误显而易见heroes.fetch(:supermann)
  • 当为哈希键的值提供私下认可值时,侧向使用 Hash#fetch 实际不是自定义逻辑。

batman = { name: 'Bruce Wayne', is_evil: false }# 差 - 如果仅仅使用 || 操作符,那么当值为假时,我们不会得到预期结果batman[:is_evil] || true # => true# 好 - fetch 在遇到假值时依然可以正确工作batman.fetch(:is_evil, true) # => false
  • 当提供暗中同意值的求值代码具有副成效或开垦异常的大时,偏向利用 Hash#fetch
    的block形式

batman = { name: 'Bruce Wayne' }# 差 - 此形式会立即求值,如果调用多次,可能会影响程序的性能batman.fetch(:powers, obtain_batman_powers) # obtain_batman_powers 开销较大# 好 - 此形式会惰性求值,只有抛出 KeyError 时,才会产生开销batman.fetch { obtain_batman_powers }

Q: Does Hash use #== or #eql? to compare hash keys?A: #eql?C: Why?
What’s the different between
them?Hash使用#eql?来做比较。而Numeric则是经过
#==做比较,而不是#eql?。在这种景色下,#eql?会做更严苛的可比。对于Object的目的,
#eql? 是跟 #== 一样的。由此可见,Numeric是分化

equal? 则检查两个是还是不是是同一对象(一样值,同样type)。

#对于 Numericx = 1puts x.object_id # => 3y = 1puts y.object_id # => 3z = 1.0puts z.object_id # => -36028797018963966x == y # true 同一个objectx.eql? y # true 同一个objectx == z #true 不同object,但是content相同x.eql? z #false 不同object,并且是不同的type,x是Fixnum,y是Float#对于 Array objectx = [1, 2, 3]y = [3, 2, 1]z = [1, 2, 3]x.object_id # => 70244601475800y.object_id # => 70244601492000x.eql? #=> falsex.object_id # => 70244601475800z.object_id # => 70244601475800x.eql? #=> truex.object_id # => 70244601475800s = y.sort # => [1, 2, 3]s.object_id # => 70244618664060x.class # => Arrayy.class # => Arrayx == s #=> true 相同valuex.eql? #=> true 相同value和typex.equal?s #=> false 相同value和type,但是不是同一个对象

Q: What’s is the precedence of &&, and, or, =, ||?A: && > || > =
>

Q: . vs ::A:
调用类方法时,他们尚无区分。不过利用::能够访谈constant和namespace

Q: !=官方澳门新永利下载, vs <=>(spaceship)A: != 不等于。
<=>,左边大于侧面则赶回1,等于重回0,小于再次回到-1

发表评论

电子邮件地址不会被公开。 必填项已用*标注