On Tue, Mar 4, 2014 at 8:08 PM, Robin Lery <robinl...@gmail.com> 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.