写了一个购买商品的功能,购了三个商品执行了 15 次查询,正常吗
写了一个购买商品的功能,大概逻辑就是:
- 查询商品是否存在,
- 创建一个总订单
- 创建最小订单(存一下商品的价格 * 数量)
- 查一下会员表,看看是否是会员,计算下折扣
- 查一下代理商表,计算下折扣
- 把总订单的价格更新一下
- 查一下小订单的表,用来做数据返回
然后看了一下查询日志,一查执行了 15 条 sql,感觉太多了,但是看一看又觉得没有不必要的查询,请问各位有什么更好的设计或解决方案吗,或者说是正常现象?
execution time: 2ms; select * from `users` where `api_token` = "X7BC0pCz6BTAxd4HhHyBD" limit 1
execution time: 1ms; select * from `commodities` where `commodities`.`id` = "1" limit 1
execution time: 0ms; select * from `commodities` where `commodities`.`id` = "2" limit 1
execution time: 0ms; select * from `commodities` where `commodities`.`id` = "3" limit 1
execution time: 0ms; select * from `shipping_addresses` where `shipping_addresses`.`user_id` = "1" and `shipping_addresses`.`user_id` is not null order by `default` desc, `id` desc limit 1
execution time: 3ms; insert into `total_orders` (`user_id`, `address`, `remark`, `delivery_method`) values (...)
execution time: 3ms; insert into `orders` (`commodity_id`, `amount`, `commodity_title`, `commodity_image`, `price`, `total_order_id`) values (...)
execution time: 2ms; insert into `orders` (`commodity_id`, `amount`, `commodity_title`, `commodity_image`, `price`, `total_order_id`) values (...)
execution time: 0ms; select * from `agents` where `user_id` = "1" and `status` = "normal" limit 1
execution time: 0ms; select * from `users` where `users`.`id` = "1" limit 1
execution time: 0ms; select * from `members` where `members`.`id` = "1" limit 1
execution time: 2ms; update `total_orders` set `total_amount` = "23940", `is_agent_order` = "", `use_discount` = "1" where `id` = "108"
execution time: 0ms; select * from `users` where `users`.`id` = "1" limit 1
execution time: 1ms; select * from `orders` where `orders`.`total_order_id` = "108" and `orders`.`total_order_id` is not null order by `id` desc
User 表相同是数据查了两次,但是看一下并不知道是在哪里查询的,难道说 Auth::id()
也会查询一次 User 表嘛
shu
1.

whereIn
-2sql批量插入 -1 sql
不知道