Skip to content

Commit

Permalink
DB など色々
Browse files Browse the repository at this point in the history
  • Loading branch information
mickamy committed Jan 3, 2024
1 parent e8daec5 commit ff918dd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
4 changes: 2 additions & 2 deletions admin/config/my.server.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ query_cache_size = 16M
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
#slow_query_log = 1
#slow_query_log_file = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
Expand Down
45 changes: 45 additions & 0 deletions note.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,48 @@ docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443 --workload 4"
2024/01/03 01:19:25 投票者の感心がなくなりました
2024/01/03 01:19:25 {"score": 193855, "success": 121527, "failure": 0}
```

- slow query log を disable 185457

```
❯ make bench
docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443 --workload 4"
2024/01/03 01:23:30 Start GET /initialize
2024/01/03 01:23:30 期日前投票を開始します
2024/01/03 01:23:31 期日前投票が終了しました
2024/01/03 01:23:31 投票を開始します Workload: 4
2024/01/03 01:24:16 投票が終了しました
2024/01/03 01:24:16 投票者が結果を確認しています
2024/01/03 01:24:31 投票者の感心がなくなりました
2024/01/03 01:24:31 {"score": 185457, "success": 115553, "failure": 0}
```

- ALTER TABLE users ADD INDEX (name, address, mynumber) 198013

```
❯ make bench
docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443 --workload 4"
2024/01/03 01:25:36 Start GET /initialize
2024/01/03 01:25:46 期日前投票を開始します
2024/01/03 01:25:47 期日前投票が終了しました
2024/01/03 01:25:47 投票を開始します Workload: 4
2024/01/03 01:26:32 投票が終了しました
2024/01/03 01:26:32 投票者が結果を確認しています
2024/01/03 01:26:34 Get https://app:443/css/bootstrap.min.css: http2: client connection force closed via ClientConn.Close
2024/01/03 01:26:34 Get https://app:443/css/bootstrap.min.css: write tcp 172.18.0.3:44244->172.18.0.2:443: write: connection reset by peer
2024/01/03 01:26:34 Get https://app:443/: http2: client connection force closed via ClientConn.Close
2024/01/03 01:26:41 Get https://app:443/css/bootstrap.min.css: write tcp 172.18.0.3:48946->172.18.0.2:443: write: broken pipe
2024/01/03 01:26:41 Get https://app:443/css/bootstrap.min.css: http2: client connection force closed via ClientConn.Close
2024/01/03 01:26:47 投票者の感心がなくなりました
2024/01/03 01:26:47 {"score": 198013, "success": 122806, "failure": 5}
```

- posts の時のクエリをまとめる

```
```





2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function run_ruby() {
cd "/home/ishocon/webapp/$app_lang"
sudo rm -rf /tmp/unicorn.pid
make_tmp_file
unicorn -c unicorn_config.rb
unicorn -c unicorn_config.rb -E production
}

function run_python() {
Expand Down
27 changes: 18 additions & 9 deletions webapp/ruby/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def db_initialize
db.query('ALTER TABLE votes ADD CONSTRAINT FOREIGN KEY (candidate_id) REFERENCES candidates (id)')
db.query('ALTER TABLE votes ADD INDEX (candidate_id, count DESC)')
# db.query('ALTER TABLE votes DROP COLUMN keyword')
db.query('ALTER TABLE users ADD INDEX (name, address, mynumber)')
db.query('ALTER TABLE votes ADD CONSTRAINT FOREIGN KEY (user_id) REFERENCES users (id)')
end
end

Expand Down Expand Up @@ -165,18 +167,25 @@ def db_initialize
end

post '/vote' do
user = db.xquery('SELECT id, votes FROM users WHERE name = ? AND address = ? AND mynumber = ?',
params[:name],
params[:address],
params[:mynumber]).first
query = <<SQL
SELECT u.id, u.votes, IFNULL(SUM(v.count), 0) as voted_count
FROM users u
LEFT OUTER JOIN votes v on u.id = v.user_id
WHERE name = ?
AND address = ?
AND mynumber = ?
GROUP BY u.id
SQL
user = db.xquery(query, params[:name], params[:address], params[:mynumber]).first
# candidate = db.xquery('SELECT * FROM candidates WHERE name = ?', params[:candidate]).first
candidate = candidates.find { |c| c[:name] == params[:candidate] }
voted_count =
user.nil? ? 0 : db.xquery('SELECT IFNULL(SUM(count), 0) AS count FROM votes WHERE user_id = ?', user[:id]).first[:count]

if voted_count.nil?
voted_count = 0
end

voted_count = user ? user[:voted_count] : 0
# user.nil? ? 0 : db.xquery('SELECT IFNULL(SUM(count), 0) AS count FROM votes WHERE user_id = ?', user[:id]).first[:count]
# if voted_count.nil?
# voted_count = 0
# end

# candidates = db.query('SELECT * FROM candidates')
cs = candidates
Expand Down

0 comments on commit ff918dd

Please sign in to comment.