MySQL Query: Which is Faster query - mysql

Method 1:
SELECT * FROM `wordpressusers` WHERE user_login='user10000001'
Result from PhpMyadmin:
Method2:
SELECT user_login FROM `wordpressusers` WHERE user_login='user10000001'
Result from PhpMyadmin:
What I think is method 2 is faster because it is selecting user_login column directly and MYSQL doesn't look to another columns. Correct me If i'm wrong.
I tried to query this in phpmyadmin and looking at the result, they are the same. I think this would make difference if the database is very big..

Actually, I expect #2 to be faster due to the reason already given.

Related

Selecting using `database_name`.`viewname`.* syntax in MySQL throws "Unknown table 'database_name.viewname'"

I'm having a strange issue with MySQL and database views.
I have a view defined like this:
CREATE VIEW circuits AS
(SELECT Id, Id AS Old_Id, Name FROM circuits_1)
UNION
(SELECT Id + 1000 AS Id, Id AS Old_Id, Name FROM circuits_2)
I have to join this view with a table that is in another database.
To do so, I usually prefix the table name with its database name, like db_name.table_name.
I've mapped this view using an ORM, specifying its prefix, and the resulting query is this one:
SELECT `webapp`.`circuits`.* FROM `webapp`.`circuits`
But this query returns this error:
#1051 - Unknown table 'webapp.circuits'
However, I've tried to manually run the query and remove the webapp. prefix from the SELECT statement, and it works as expected, throwing no error at all
SELECT `circuits`.* FROM `webapp`.`circuits`
Any idea why this happens?
Is it related to the way the view is defined?
EDIT
Another strange thing:
Even if this query fails:
SELECT `webapp`.`circuits`.* FROM `webapp`.`circuits`
This doesn't:
SELECT `webapp`.`circuits`.Id FROM `webapp`.`circuits`
I was hesitant to answer, as i am not familiar enough with mysql to give a full answer. I did some testing on rextester.com though, and found the following:
If I create a table test(id int), I can query it using its fully qualified object name:
SELECT rextester.test.*
FROM rextester.test
Works, no problem.
If I create a view so_test as (Select 1 id from dual)
I cannot do the same:
SELECT rextester.so_test.*
FROM rextester.so_test
Returns the same error you get.
I cannot conclude too much from this, as i don't know mysql well enough. However, it seems a general issue with views, not the way you created it.
MySQL does not seam to support * rewrite to the matching table columns with in the VIEW.
MySQL 5.6.39
http://sqlfiddle.com/#!9/68f2d3/4
MySQL 5.7
https://www.db-fiddle.com/f/taRV6FLAP6Mf8oMeuniZP3/2
MySQL 8.0.11
https://www.db-fiddle.com/f/taRV6FLAP6Mf8oMeuniZP3/3

mysql triggers using vs select query

I use mysql trigger to update column in one of ,y DB tables called comments_count but I want to know what is best and faster??
Using mysql triggers or select query like this below:
select count(*) from comments where discussion_id=something
different types of overhead:
with the trigger you will have extra time during insert, and may get out of synch over time for some unforseen reason.
with the query, you will always get the right answer but you will need to calculate at runtime. usually, this should be very fast especially with an index on the discussion_id

Can't specify target table for update in FROM clause - Mysql 5.7.10 - derived_merge not working

I have the following query:
update tblwerknemerdienst toUpdate
set datumtot = (select subdate(datumvanaf,1)
from (select * from tblwerknemerdienst) nextDienst
where nextDienst.Werknemer = toUpdate.Werknemer
and nextDienst.datumvanaf > toUpdate.DatumVanaf
order by DatumVanaf
LIMIT 1)
WHERE DatumTot IS NULL;
The query runs fine on MySql versions other than MySql 5.7.10. I've searched around the web and found that you have to set derived_merge=off, but sadly this had no effect and the query still fails with the same error.
I have also tried several different ways of rewriting the query, but all to no avail.
Is there something I'm missing or is there another way to accomplish this?
In the end I fixed this by rewriting the whole thing in a procedure, where I used a cursor to execute the query and get the necessary data. Then I perform the update statement based upon the fields selected in the cursor.
This seemed to be the only way to reliably perform the operation required on different versions of MySql.

MySQL inserting with select max(`Column`)+1 on possibly empty results

I've seen a lot of questions along the same-ish lines as mine, but they're just not quite exactly the same issue as me.
Here's my query:
insert into`quotes`(`QuoteID`,`QuoteRequestID`,`Number`,`UserID`,`Viewed`,
`_Latest`)
select uuid(),uuid(),
ifnull(max(`quotes`.`Number`),0)+1,'Some User ID',1,1
from`quotes`
join`quoterequests`using(`QuoteRequestID`)
where`quoterequests`.`UserID`='Some User Other ID';
And this is the error I'm getting:
Error Code: 1048. Column 'QuoteID' cannot be null
So my guess is that the select statement isn't returning anything, but how can that be since I'm using max() in my query?
Sure enough, if I remove the insert part and just run the select statement by itself, then I do get the expected results, with the new UUIDs (definitely not a null value) and all. What is happening here? Is this a bug with MySQL? My MySQL version is 5.7.14;
EDIT
So I have figured out if I wrap the select in another select, it now works as expected. Not sure if that's a solution or a workaround though, but it did get my query working.
it looks like you might have some trigger which generates the error - that is the only explanation I can think about.

Mysql table exists, but when shows 'It does not exist' when SELECT query is performed

I have a MySql table named options in the database. It is listed in the table list. When I perform the query show tables then it is being displayed in the table list. But when I perform select * from options I am getting an error that Table 'stillmanmvolvo.options' doesn't exist. Please suggest me what to do.
Thank you
Try this if options is still there:
SHOW TABLES FROM `stillmanmvolvo`;
If it's there, try this:
USE `stillmanmvolvo`;
Then:
SELECT * FROM `options`;
Don't forget the backticks!