What are the performance characteristics of caret or $ in a MySQL REGEXP?

Query the list of CITY names from STATION which have vowels (i.e., a,
e, i, o, and u) as both their first and last characters. Your result
cannot contain duplicates.

Solution:

SELECT DISTINCT CITY
FROM STATION
WHERE 
    CITY REGEXP '^[aeiou]' AND
    CITY REGEXP '[aeiou]$'

Why was this solution downvoted a lot in a forum? Is there any performance issue with a query like that, or is it bad practice somehow?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

No, there is no performance issue with using these. They are instead faster way of searching strings in comparison to nested queries. On HackerRank forum, I have seen many other course comments also where answers were right but people were just downvoting. Though the two lines in ‘where city REGEXP’ part can be combined and written in one line, the time complexity for both will be same. If you want to know more about Regex in MySQL, you can follow this post :-

https://www.geeksforgeeks.org/mysql-regular-expressions-regexp/


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x