Re: confused about sql in django

2014-03-05 Thread Robin Lery
Thank you Alex and Tom! Its great to know something new. I think I will
stick with django orm for now! Thanks again!


On Wed, Mar 5, 2014 at 2:48 AM, Tom Evans  wrote:

> On Tue, Mar 4, 2014 at 8:08 PM, Robin Lery  wrote:
> > Hello,
> >
> > I am really being confused. How to use sql in django? I found two:
> > 1. Performing raw queries.
> > 2.Executing custom SQL directly.
> >
> > Does it have better performance than the django's orm? And how is the
> > performance the same between those two?
>
> 1 and 2 are the same thing. Running the same query using the ORM and
> running the same query directly will take the same time - the clue is
> in the name, it is the *same* query.
>
> Running queries that return model instances is slower using the ORM
> than simply fetching the data row at a time, simply because django
> will construct model instances from each row of data, which costs
> time.
>
> There are queries that django's ORM cannot express, for those
> obviously you must use SQL directly.
>
> The ORM will not write "bad" or slow queries, there is always a
> possibility that a query could be written better by hand. The ORM is
> powerful, but this power does allow you to write queries that look
> simple in code, but actually do complex and unexpected things when
> run. Of course, the same is true of writing SQL by hand.
>
> I think it is important to understand that the two do different
> things. ORM stands for Object Relational Mapper, which means it is
> perfect for extracting objects that are related to each other. SQL
> stands for Structured Query Language, which means it can be used to
> extract almost any data in whatever format you require. If you want
> objects, use the ORM, if you want arbitrary data, use SQL.
>
> Using an ORM does not mean that you no longer have to design and
> optimise your database and the queries you run on it. You still need
> to check your queries to ensure that your DB engine is optimising them
> correctly, that you have the correct indices on the columns you are
> querying on - probably also that you aren't repeating the same read
> queries over and over again within one request.
>
> >
> > And lastly, what is sqlalchemy? Are sqlalchemy and django's orm the
> same? In
> > the website of sqlalchemy it says "SQLAlchemy is the Python SQL toolkit
> and
> > Object Relational Mapper that gives application developers the full power
> > and flexibility of SQL".
>
> sqlalchemy is a different ORM to django. All ORMs do basically the
> same thing under the hood, so there are obvious similarities between
> the two. sqlalchemy has a few more features than django's ORM, at the
> cost of being more complex.
>
> You can use sqlalchemy in django instead of django's ORM/models, but
> you lose the ability to use anything based on django models - the
> admin site, authentication, basically anything that touches the
> database. You can still use templates, i18n, some session backends,
> forms (but not model forms), views and url handling.
>
> Cheers
>
> Tom
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAFHbX1%2B7nkk-4Mh1MD03aHZyJnoNwAFOi%3DHiie7ahpg6iCBHUQ%40mail.gmail.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2B4-nGpGPLpTP7uRv7PrNRp1CwEiCLy-p1O9BUuU0Yc%3DDCLfOA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: confused about sql in django

2014-03-04 Thread Tom Evans
On Tue, Mar 4, 2014 at 8:08 PM, Robin Lery  wrote:
> Hello,
>
> I am really being confused. How to use sql in django? I found two:
> 1. Performing raw queries.
> 2.Executing custom SQL directly.
>
> Does it have better performance than the django's orm? And how is the
> performance the same between those two?

1 and 2 are the same thing. Running the same query using the ORM and
running the same query directly will take the same time - the clue is
in the name, it is the *same* query.

Running queries that return model instances is slower using the ORM
than simply fetching the data row at a time, simply because django
will construct model instances from each row of data, which costs
time.

There are queries that django's ORM cannot express, for those
obviously you must use SQL directly.

The ORM will not write "bad" or slow queries, there is always a
possibility that a query could be written better by hand. The ORM is
powerful, but this power does allow you to write queries that look
simple in code, but actually do complex and unexpected things when
run. Of course, the same is true of writing SQL by hand.

I think it is important to understand that the two do different
things. ORM stands for Object Relational Mapper, which means it is
perfect for extracting objects that are related to each other. SQL
stands for Structured Query Language, which means it can be used to
extract almost any data in whatever format you require. If you want
objects, use the ORM, if you want arbitrary data, use SQL.

Using an ORM does not mean that you no longer have to design and
optimise your database and the queries you run on it. You still need
to check your queries to ensure that your DB engine is optimising them
correctly, that you have the correct indices on the columns you are
querying on - probably also that you aren't repeating the same read
queries over and over again within one request.

>
> And lastly, what is sqlalchemy? Are sqlalchemy and django's orm the same? In
> the website of sqlalchemy it says "SQLAlchemy is the Python SQL toolkit and
> Object Relational Mapper that gives application developers the full power
> and flexibility of SQL".

sqlalchemy is a different ORM to django. All ORMs do basically the
same thing under the hood, so there are obvious similarities between
the two. sqlalchemy has a few more features than django's ORM, at the
cost of being more complex.

You can use sqlalchemy in django instead of django's ORM/models, but
you lose the ability to use anything based on django models - the
admin site, authentication, basically anything that touches the
database. You can still use templates, i18n, some session backends,
forms (but not model forms), views and url handling.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1%2B7nkk-4Mh1MD03aHZyJnoNwAFOi%3DHiie7ahpg6iCBHUQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: confused about sql in django

2014-03-04 Thread Alex Mandel
On 03/04/2014 12:08 PM, Robin Lery wrote:
> Hello,
> 
> I am really being confused. How to use sql in django? I found two:
> 1. Performing raw queries.
> 2.Executing custom SQL directly.
> 
Do you have links for those? I know there is a the raw query mode of
Django, and I'm guessing the other method you refer to is creating your
own db connection directly with the python driver for a particular
database? The raw queries will at least be more similar to the results
you get from the ORM in terms of object types returned, where direct SQL
would require you to take the results, and iterate them into a useful
object.

> Does it have better performance than the django's orm? And how is the
> performance the same between those two?
> 

Probably not better performing than the ORM, I expect the ORM to behave
better. The key is the ORM returns results in an easy to use object that
can be then passed to the templates. It also has options to pre-cache
some results, only query when you actually need it, etc.
However there are some types of queries the ORM can't do, that's
typically when you need the direct SQL methods.

> And lastly, what is sqlalchemy? Are sqlalchemy and django's orm the same?
> In the website of sqlalchemy it says "*SQLAlchemy is the Python SQL toolkit
> and Object Relational Mapper that gives application developers the full
> power and flexibility of SQL*".
> 

No, Django ORM is a competitor of SQL Alchemy. Though you can use either
or both it's just more work for you.


> So I am confused. I will really appreciate very much, if you could help me
> understand the above.
> 
> Thank you!
> 

Enjoy,
Alex

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5316376D.6040808%40wildintellect.com.
For more options, visit https://groups.google.com/groups/opt_out.


confused about sql in django

2014-03-04 Thread Robin Lery
Hello,

I am really being confused. How to use sql in django? I found two:
1. Performing raw queries.
2.Executing custom SQL directly.

Does it have better performance than the django's orm? And how is the
performance the same between those two?

And lastly, what is sqlalchemy? Are sqlalchemy and django's orm the same?
In the website of sqlalchemy it says "*SQLAlchemy is the Python SQL toolkit
and Object Relational Mapper that gives application developers the full
power and flexibility of SQL*".

So I am confused. I will really appreciate very much, if you could help me
understand the above.

Thank you!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2B4-nGqCeFpw77DnoZ_uykEb1PMisv0Z6GcxPpj_WHU7LhLC6w%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.