0

I have two tables clients and shows.

Clients Table

+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| clientID    | int          | NO   | PRI | NULL    |       |
| name        | varchar(20)  | YES  |     | NULL    |       |
| routine     | varchar(30)  | YES  |     | NULL    |       |
| standardFee | decimal(5,2) | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+

Shows Table

+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| showID      | int          | NO   | PRI | NULL    |       |
| showDate    | date         | YES  |     | NULL    |       |
| clientID    | int          | YES  | MUL | NULL    |       |
| attendance  | int          | YES  |     | NULL    |       |
| ticketPrice | decimal(4,2) | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+

The following is the data in the tables.

+----------+------------+------------------+-------------+
| clientID | name       | routine          | standardFee |
+----------+------------+------------------+-------------+
|        1 | Gladys     | song and dance   |      140.00 |
|        2 | Katherine  | standup          |      140.00 |
|        3 | Georgeanna | standup          |      140.00 |
|        4 | Wanda      | song and dance   |      140.00 |
|        5 | Ann        | trained squirrel |       79.99 |
+----------+------------+------------------+-------------+
+--------+------------+----------+------------+-------------+
| showID | showDate   | clientID | attendance | ticketPrice |
+--------+------------+----------+------------+-------------+
|      1 | 2019-12-25 |        4 |        144 |       22.50 |
|      2 | 2020-01-11 |        5 |         84 |       29.50 |
|      3 | 2020-01-17 |        3 |        190 |       19.49 |
|      4 | 2020-01-31 |        5 |        234 |       14.99 |
|      5 | 2020-02-08 |        1 |         86 |       25.00 |
|      6 | 2020-02-14 |        3 |        122 |       59.25 |
|      7 | 2020-02-15 |        2 |        101 |       26.50 |
|      8 | 2020-02-27 |        2 |        186 |       19.99 |
|      9 | 2020-03-06 |        4 |        222 |       45.00 |
|     10 | 2020-03-07 |        5 |        250 |        8.99 |
+--------+------------+----------+------------+-------------+

I am trying to understand the following query

EXPLAIN UPDATE 
clients JOIN shows 
USING (clientID) 
SET clients.routine = 'mime', 
shows.ticketPrice = 30 
WHERE name = 'Gladys' or name = 'Ann';

MySql returned the following

+----+-------------+---------+------------+------+---------------+-----------+---------+---------------------------+------+----------+-------------+
| id | select_type | table   | partitions | type | possible_keys | key       | key_len | ref                       | rows | filtered | Extra       |
+----+-------------+---------+------------+------+---------------+-----------+---------+---------------------------+------+----------+-------------+
|  1 | UPDATE      | clients | NULL       | ALL  | PRIMARY       | NULL      | NULL    | NULL                      |    5 |    36.00 | Using where |
|  1 | UPDATE      | shows   | NULL       | ref  | client_fk     | client_fk | 5       | updatedb.clients.clientID |    1 |   100.00 | NULL        |
+----+-------------+---------+------------+------+---------------+-----------+---------+---------------------------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

I am wondering why mysql went through only 1 row for the shows table when the update happened on Four of its rows where clientID is 5 and clientID is 1.

I went through how indexes work. I am finding it difficult to understand query execution on two tables when Indexes are involved.

1 Answers1

0

With MySQL, if there's no change to the row nothing happens - it's a no-op.

Only the first time shows is updated the ticket price is set to 30, but the remaining 4 times no shows column values are affected by the update, so no actual "update" occurs.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thank you for your inputs. I have a doubt here. After I executed the query the column ticketPrice gets updated where clientID is 1 or 5. There are 4 such rows in the Shows table which are getting updated. Correct me if I am wrong. – ShrihariShetty Mar 12 '23 at 09:11