On my authorization, i was looking user by his social id:
select * from users where yandex_id = 65250508;
And result is very bad: 1 row in set (11.25 sec)
Count of this table:
select count(id) from users;
+-----------+
| count(id) |
+-----------+
| 1852446 |
+-----------+
Also there is explain of my query:
explain select * from users where yandex_id = 65250508;
+------+-------------+-------+------+---------------------------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------------------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | users | ALL | UNIQ_1483A5E988FDD79D,yandex_id | NULL | NULL | NULL | 1820017 | Using where |
+------+-------------+-------+------+---------------------------------+------+---------+------+---------+-------------+
and describe of table:
describe users;
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ts | int(10) unsigned | NO | MUL | NULL | |
| last_ts | int(10) unsigned | NO | MUL | NULL | |
| last_mail | int(10) unsigned | NO | | NULL | |
| photo | varchar(32) | YES | | NULL | |
| name | varchar(48) | YES | | NULL | |
| email | varchar(48) | YES | UNI | NULL | |
| state | smallint(6) | NO | MUL | NULL | |
| ip | bigint(20) unsigned | NO | | NULL | |
| gender | varchar(1) | NO | | NULL | |
| facebook_id | varchar(64) | YES | UNI | NULL | |
| mailru_id | varchar(64) | YES | UNI | NULL | |
| vk_id | varchar(64) | YES | UNI | NULL | |
| yandex_id | varchar(64) | YES | UNI | NULL | |
| google_id | varchar(64) | YES | UNI | NULL | |
| roles | longtext | YES | | NULL | |
| is_active | tinyint(1) | NO | MUL | 1 | |
+-------------+---------------------+------+-----+---------+----------------+
17 rows in set (0.00 sec)
Try USE INDEX(update_index) in your query explicitly.
Sometimes the optimizer makes wrong choice in selecting the index because of which the query is becoming slow.
I have resolved my issue. So, problem was in that i'm using in where integer value, but my field vas defined as varchar, so when i have changed searching id to string, it starts working perfect
MariaDB [hrabr]> select * from users force index(yandex_id) where yandex_id = 65250508;
1 row in set (13.81 sec)
and with string:
MariaDB [hrabr]> select * from users force index(yandex_id) where yandex_id = '65250508';
1 row in set (0.00 sec)
I hope it will help someone!
Related
I can't explain this issue because i don't know whats wrong...
Here is my table:
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| u_id | int(11) | NO | | NULL | |
| UA | varchar(200) | YES | | NULL | |
| IP | varchar(32) | YES | | NULL | |
| Cookie | varchar(32) | NO | | NULL | |
| ConnectionID | varchar(40) | NO | | NULL | |
| timestamp | int(15) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
This is my query for MySQL
UPDATE u_jar
SET u_id = 13, `timestamp` = 1460040908
WHERE cookie = '64b2b43e343855019fecf6c89751f884'
and I'm getting
Column count doesn't match value count at row 1
This question already has answers here:
Return row only if value doesn't exist
(2 answers)
Closed 7 years ago.
The foreign key trade.ticker_id point to ticker.id.
I want to find all rows in 'ticker' that have zero rows in 'trades' linked to it.
mysql> describe trade;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| time | datetime | NO | | NULL | |
| price | float | NO | | NULL | |
| quantity | int(11) | NO | | NULL | |
| source | varchar(64) | YES | | NULL | |
| buyer | varchar(64) | YES | | NULL | |
| seller | varchar(64) | YES | | NULL | |
| initiator | varchar(64) | YES | | NULL | |
| ticker_id | int(11) | YES | MUL | NULL | |
+-----------+-------------+------+-----+---------+----------------+
9 rows in set (0,00 sec)
mysql> describe ticker;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(64) | NO | | NULL | |
| long_name | varchar(250) | YES | | NULL | |
| exchange_id | int(11) | YES | MUL | NULL | |
+-------------+--------------+------+-----+---------+----------------+
4 rows in set (0,00 sec)
Try this:
SELECT * FROM ticker WHERE id NOT IN (SELECT ticker_id FROM trade)
You can use left join and is null or in other words anti join
select tc.* from ticker tc
left join trade tr on tr.ticker_id = tc.id
where tr.ticker_id is null
How can I select all rows from the 'trade' table that have a 'ticker_id' field equal to the 'id' field of a row from the 'ticker' table, that has a 'name' field equal to 'MSFT'?
The 'trade.ticker_id' field is a foreign key to the 'ticker.id' field.
Is this a bad design if I want filter a lot of 'trade' rows?
mysql> describe ticker;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(64) | NO | | NULL | |
| long_name | varchar(250) | YES | | NULL | |
| exchange_id | int(11) | YES | MUL | NULL | |
+-------------+--------------+------+-----+---------+----------------+
4 rows in set (0,00 sec)
mysql> describe trade;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| time | datetime | NO | | NULL | |
| price | float | NO | | NULL | |
| quantity | int(11) | NO | | NULL | |
| source | varchar(64) | YES | | NULL | |
| buyer | varchar(64) | YES | | NULL | |
| seller | varchar(64) | YES | | NULL | |
| initiator | varchar(64) | YES | | NULL | |
| ticker_id | int(11) | YES | MUL | NULL | |
+-----------+-------------+------+-----+---------+----------------+
9 rows in set (0,00 sec)
SELECT * FROM trade TR
LEFT JOIN ticker TI ON TR.ticker_id = TI.id
WHERE TI.name = "MSFT"
i have a 'results' table that has a datetime colum 'created_time'.
this table reference another table 'results_values' (results.id reference results_values.results_id). here are tables describe:
mysql> describe webforms_results;
+------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| webform_id | int(11) | NO | | NULL | |
| store_id | int(11) | NO | | NULL | |
| customer_id | int(11) | NO | | NULL | |
| customer_ip | bigint(20) | NO | | NULL | |
| created_time | datetime | NO | | NULL | |
| update_time | datetime | NO | | NULL | |
| approved | tinyint(1) | NO | | NULL | |
| view_on_frontend | tinyint(1) | NO | | 0 | |
+------------------+------------+------+-----+---------+----------------+
mysql> describe webforms_results_values;
+--------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| result_id | int(11) | NO | MUL | NULL | |
| field_id | int(11) | NO | | NULL | |
| value | text | NO | | NULL | |
| rating | int(11) | YES | | NULL | |
| created_time | datetime | YES | | NULL | |
+--------------+----------+------+-----+---------+----------------+
now i've added a datetime column 'created_time' on 'results_values', and i want to insert datetimes on the second tables copying them from the first table on the corresponding id. is there a quick way to do it?
yes triggers are the solution. And if you want to update already inserted data, you can do :
UPDATE webforms_results_values wrv INNER JOIN webforms_results wr
ON wrv.result_id = wr.id
SET wrv.created_time = wr.created_time
Create "Triggers" to update each and every update of those tables.
Please refer following URL:
Triggers
I am trying to write a MySQL query that gives me results of Organisation Name, its Post Code, any Events that belong to the Organisation and the Post Code of that Event. I've tried all sorts of of join, join and select combinations to no avail. Is this something that is possible ? (I could have a separate table for Org Address and Event Address but it seems like it should be possible to use just one table)
My table structures:
mysql> DESCRIBE cc_organisations;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| type | enum('C','O') | YES | | NULL | |
| name | varchar(150) | NO | MUL | NULL | |
| description | text | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> DESCRIBE cc_events;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| org_id | int(10) unsigned | NO | MUL | NULL | |
| name | varchar(150) | NO | MUL | NULL | |
| start_date | int(11) | NO | MUL | NULL | |
| end_date | int(11) | YES | MUL | NULL | |
| start_time | int(11) | NO | | NULL | |
| end_time | int(11) | NO | | NULL | |
| description | text | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
mysql> DESCRIBE cc_addresses;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| org_id | int(10) unsigned | YES | MUL | NULL | |
| event_id | int(10) unsigned | YES | MUL | NULL | |
| post_code | varchar(7) | NO | MUL | NULL | |
| address_1 | varchar(100) | NO | | NULL | |
| address_2 | varchar(100) | YES | | NULL | |
| town | varchar(50) | NO | | NULL | |
| county | varchar(50) | NO | | NULL | |
| email | varchar(150) | NO | | NULL | |
| phone | int(11) | YES | | NULL | |
| mobile | int(11) | YES | | NULL | |
| website_uri | varchar(150) | YES | | NULL | |
| facebook_uri | varchar(250) | YES | | NULL | |
| twitter_uri | varchar(250) | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
14 rows in set (0.00 sec)
select o.Name, oAddress.PostCode, e.Name, eAddress.PostCode
from cc_organisations o
inner join cc_addresses oAddress on oAddress.org_id = o.id
left outer join cc_events e on e.org_id=o.id
inner join cc_addresses eAddress on eAddress.event_id = e.id
SELECT cco.name as OrgName, cca.post_code as OrgPostCode, cce.id,
cce.org_id, cce.name, cce.start_date, cce.end_date, cce.start_time,
cce.end_time, cce.description
FROM cc_events cce, cc_addresses cca, cc_organisations cco
WHERE cca.event_id = cce.id AND cco.id=cce.org_id
ORDER BY cce.start_date
LIMIT 50;
You can change your sort and limit, I just added those in because I don't know how big your DB is... You may even be able to get away with:
SELECT cco.name as OrgName, cca.post_code as OrgPostCode, cce.*
FROM cc_events cce, cc_addresses cca, cc_organisations cco
WHERE cca.event_id = cce.id AND cco.id=cce.org_id
ORDER BY cce.start_date LIMIT 50;
But im not 100% sure if the 2nd query will bum out or not.
Your address table has the post codes in it; but it also has an organization id and event id foreign keys. We only need to check the event_id from the address table because any event will belong to an organization.
Address's Event matched Event ID
Event's Organization matched Organization ID