Asking For SQL Tips From OpenAI ChatGPT

avatar
(Edited)

So I decided to try out the latest hot new thing with OpenAI ChatGPT.

Since I use SQL to query things related to Hive, I decided to ask for a tips how to improve one of my queries.

It is is a simple query..... here it is:

image.png

https://chat.openai.com/chat

It gave me four tips!
Nice!

  1. The use or no use of the NOLOCK statement is something I'm aware of, but I do it intentionally, since the database is being updated continuously as the chain goes.

  2. Use index is also something I'm aware of, but till now the reading times for this query has been fast and there haven't been need to do it. I might implement it in some other queries though.

  3. Using parameters, as example for dates, might be a good thing, but its not that hard for me to change it directly in the date section instead of having separate parameters where to change it.

  4. Optimize the query. Arranging the lines in proper order might be useful and result in faster execution.

So there you go. You can now ask AI to give you coding tips and review your code!

The tips above are not essential for the query to work, but overall they are all good tips for implementing a proper working practices.

At least I know I haven't done some super dumb thing :)



0
0
0.000
12 comments
avatar

The second tip cannot be done on the select query it is needed to be done in the database structure.

So Ai needs to learn the difference between query optimization vs database optimization

0
0
0.000
avatar

It has a lot to learn, in general. But if you have access to add indexes, the tip is good.

0
0
0.000
avatar

Yes it is.

That kind of tip it is the same that payed "optimization" programa gives to you.

0
0
0.000
avatar

I have gave it a very little to work with overall.... should define the table first, but decided to give it a go and see what it gives when you just paste some query there

0
0
0.000
avatar

I know that 😉 and it is a very good test to see how Ai will reply to it

0
0
0.000
avatar

They have an AI trained to generate Python code from natural language. It's in the examples of OpenAI.

0
0
0.000
avatar

Yea I have seen examples to ask it to write all sort of code and it will be give something... probably you need a review it, but it can be extremely helpful

0
0
0.000
avatar

the title looks like you just rolled your faced on the keyboard with all those acronyms ,upper and lower case letters :P

0
0
0.000
avatar

The use or no use of the NOLOCK statement is something I'm aware of, but I do it intentionally, since the database is being updated continuously as the chain goes.

This is forced on HiveSQL, as it is in the view.

0
0
0.000
avatar

So the ai was right :)
But how can I know that there is a forced use of this if I'm not the one giving access to the tables?

0
0
0.000
avatar

You are not actually accessing the tables. You are querying against views that were created to force no lock and potentially other things.

0
0
0.000