soudai / isucon10-qualify

今年こそちゃんと予選突破するんじゃ!!!!!
3 stars 0 forks source link

DBまとめ #9

Closed soudai closed 3 years ago

soudai commented 4 years ago
mysql> show tables;

スロークエリログを出す

slow_query_log                = 1
slow_query_log_file           = /var/lib/mysql/mysqld-slow.log
long_query_time               = 0
log-queries-not-using-indexes = 1
soudai commented 4 years ago

https://github.com/sonots/isucon3_cheatsheet/blob/master/06.mysql_5.6.md

soudai commented 4 years ago

https://github.com/sonots/isucon3_cheatsheet/blob/master/06.mysql_stat.md

soudai commented 4 years ago
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)
soudai commented 4 years ago

bot からのリクエスト

bot からのアクセスはコンバージョンに繋がらないため、弾くことが仕様として決定しましたが、まだ実装されていません。 bot は User-Agent が以下の正規表現にマッチする形式であり、このリクエストに対して 503 Service Unavailable を返すことが許可されています。 これに対するベンチマーカーからの減点は発生しません。

/ISUCONbot(-Mobile)?/
/ISUCONbot-Image\//
/Mediapartners-ISUCON/
/ISUCONCoffee/
/ISUCONFeedSeeker(Beta)?/
/crawler \(https:\/\/isucon\.invalid\/(support\/faq\/|help\/jp\/)/
/isubot/
/Isupider/
/Isupider(-image)?\+/
/(bot|crawler|spider)(?:[-_ .\/;@()]|$)/i
soudai commented 4 years ago
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| isuumo             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> use isuumo
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables_in_isuumo |
+------------------+
| chair            |
| estate           |
+------------------+
2 rows in set (0.00 sec)
soudai commented 4 years ago

tableが二個しか無い。

soudai commented 4 years ago
mysql> show tables;
+------------------+
| Tables_in_isuumo |
+------------------+
| chair            |
| estate           |
+------------------+
2 rows in set (0.00 sec)

mysql> select count(*) from chair;
+----------+
| count(*) |
+----------+
|    29500 |
+----------+
1 row in set (0.01 sec)

mysql> select count(*) from estate;
+----------+
| count(*) |
+----------+
|    29500 |
+----------+
1 row in set (0.01 sec)
soudai commented 4 years ago
mysql> show index from chair\G
*************************** 1. row ***************************
        Table: chair
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 28866
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
1 row in set (0.00 sec)

mysql> show index from estate\G
*************************** 1. row ***************************
        Table: estate
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 30
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
1 row in set (0.00 sec)
soudai commented 4 years ago
  get '/api/estate/low_priced' do
    sql = "SELECT * FROM estate ORDER BY rent ASC, id ASC LIMIT #{LIMIT}" # XXX:
    estates = db.xquery(sql).to_a
    { estates: estates.map { |e| camelize_keys_for_estate(e) } }.to_json
  end

limitなので rent + id の組み合わせのINDEXでORDER BY狙いのINDEXが聞きそう

soudai commented 4 years ago
         id: 1
       name: ISU小林レジデンス
description: 又国内思想指導の方針としては、較もすれば党派的に陥る全体主義ではなくして、何処までも公明正大なる君民一体、万民翼賛の皇道でなければならない。
  thumbnail: /images/estate/eea2d2162cc9bf2c838f8b966fa716a967452e7ed62db7463763fc34a310a1d8.png
    address: 茨城県港区台東3丁目20番18号 シャルム三ノ輪360
   latitude: 37.71778813321466
  longitude: 140.2540394463388
       rent: 138843
door_height: 173
 door_width: 77
   features: 防犯カメラ,敷地内ゴミ置場
 popularity: 691612
1 row in set (0.00 sec)

ERROR:
No query specified
soudai commented 4 years ago
mysql> select * from chair limit 1 \G
*************************** 1. row ***************************
         id: 1
       name: 【伝説の一品】普通のソファー
description: 音楽堂の近くで、黒と白の法被がわたしのそばに来るなと思う間もなく、ウェッシントン夫人のいつもの歎願の声が耳のそばに聞こえた。
  thumbnail: /images/chair/136947c07cbda8cd7effdd92b183498ace80eb658519f5e41a42443b32bdc760.png
      price: 12901
     height: 197
      width: 143
      depth: 31
      color: ベージュ
   features: 木製,中国製
       kind: 座椅子
 popularity: 502728
      stock: 7
1 row in set (0.00 sec)
soudai commented 4 years ago

https://github.com/soudai/isucon10-qualify/blob/master/webapp/ruby/app.rb#L478

なぞってAPI

ここがDB的には核心の一つっぽい

    # 四角で指定して、対象の店舗をとってくる
    sql = 'SELECT * FROM estate WHERE latitude <= ? AND latitude >= ? AND longitude <= ? AND longitude >= ? ORDER BY popularity DESC, id ASC'
    estates = db.xquery(sql, bounding_box[:bottom_right][:latitude], bounding_box[:top_left][:latitude], bounding_box[:bottom_right][:longitude], bounding_box[:top_left][:longitude])

    estates_in_polygon = []
    # 四角の対象に対してN+1
    estates.each do |estate|
      ## 店舗の座標を入れる
      point = "'POINT(%f %f)'" % estate.values_at(:latitude, :longitude)
     ## 対象の範囲を元に面にする
      coordinates_to_text = "'POLYGON((%s))'" % coordinates.map { |c| '%f %f' % c.values_at(:latitude, :longitude) }.join(',')
      ## 面に対して対象の店舗が含まれているか確認する
      sql = 'SELECT * FROM estate WHERE id = ? AND ST_Contains( (%s), ST_GeomFromText(%s))' % [coordinates_to_text, point]
      e = db.xquery(sql, estate[:id]).first
      if e
        estates_in_polygon << e
      end
    end

N+1だし、座標計算を使ってるのでMySQLがめちゃ重い

soudai commented 4 years ago
root@team286-001:~# mysql -u isucon -h 10.162.86.102 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.21 MySQL Community Server - GPL
soudai commented 4 years ago

https://dev.mysql.com/doc/refman/5.6/ja/creating-spatial-indexes.html

これでINDEX春だけでも違うのでは?