LINQ to SQL ÐÔÄÜ 10 Tips
LINQ to SQL ÐÔÄÜ 10 Tips
http://www.cnblogs.com/worksguo/archive/2008/06/04/1213075.html
Ç°Ò»ÖÜ£¬ÎÒµÄÓ²ÅÌÓÐÁ½·ÖÇø»µÁË£¬ÎÒ×¼±¸µÄÎÄÕÂÓë×ÊÁ϶¼ÔÚÀïÃ棬ËùÒÔLINQµÄÎÄÕÂÍ£ÁËÒ»¶Îʱ¼ä£¬ÕæµÄÌ«²»ºÃÀ£¬ÎªÀ´ÃÖ²¹Ò»Ï¾ÍÏÈ·¢ÕâƪÎÄÕÂÉÏÀ£¬Ã÷ÌìÔÙһƪ¹ØÓÚDisconnection DataµÄÎÄÕ¡£
Ö»²»¹ýÐҺã¬ÔÚÕ⼸ÌìÖÐÎҽ߾¡È«Á¦»Ö¸´ÕâЩÊý¾Ý£¬¾¹ý3ÌìµÄŬÁ¦ÖÕÓÚÊÇÈ«²¿ÕÒ»ØÀ´À¡£
ÏÂÃæÊÇһЩÌÖÂÛLINQ to SQLÐÔÄÜһЩÎÄÕ¡£
http://blogs.msdn.com/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx
http://www.jdconley.com/blog/archive/2007/11/28/linq-to-sql-surprise-performance-hit.aspx
"LINQ Changes How You Will Program" by Bill McCarthy.
"Layer Data Access with LINQ to SQL" by Roger Jennings.
1:¹Ø±ÕOjectTrackingEnabledÊôÐÔ.
Èç¹ûÄãÖ»ÊÇÕÒ»ØÊý¾Ý,²¢ÇÒ²»ÐÞ¸ÄÈκζ«Î÷,Äã²»ÐèÒªobjectTracking,ËùÓоÍÏñÏÂÃæÒ»Ñù:
using (NorthwindDataContext context = new NorthwindDataContext())
{
context.ObjectTrackingEnabled = false;
}
2:²»Òª½«ÄãµÄËùÓÐDB Object¶¼·ÅÈëµ¥¸öDataContextÖÐ.
DataContext±íʾһ¸öµ¥¶ÀµÄ¹¤×÷µ¥Ôª,¶ø²»ÊÇÄãÊý¾Ý¿âÈ«²¿.ÈçÈôÄãÓжà¸öûÓÐÁ¬½ÓµÄÊý¾Ý¿â¶ÔÏó,»òËûÃÇûÓб»Ê¹ÓÃ.ÕâЩ¶ÔÏó¾Í²»ÐèÒªÌí¼Ó½øÄÚ´æÖеıêʶ¹ÜÀíÖÐÏûºÄµÄÄÚ´æ¿Õ¼äºÍÔÚDataContextµÄCUD engine¸ú×Ù¶ÔÏóµÄ»¨·Ñ.
ÎÒ½¨ÒéÄ㽫ÄãµÄ¹¤×÷Êý¾ÝÇøÓò·Ö¸î¼¸¸öDataContexts,ÿһ¸öÇøÓò±íʾһ¸öµ¥¶ÀµÄʹÓÃÊý¾ÝµÄ¹¤×÷µ¥Ôª.Ä㻹ÊÇÄÜʹÓÃͬһ¸öÁ¬½Ó.
3:²»ÒªÊ¹Óà CompiledQuery
µ±Äã´´½¨ºÍÖ´ÐÐÄãµÄ²éѯ,´Óexpressionµ½ÏàÓ¦µÄSQL,Óм¸¸ö²½Öè:
1.
Create expression tree(´´½¨±í´ïÊ÷)
2.
Convert it to SQL(ת»»SQL)
3.
Run the query(ÔËÐвéѯ)
4.
Retrieve the data(ÕÒ»ØÊý¾Ý)
5.
Convert it to the objects(ת»»Îª¶ÔÏó)
Äã×¢Òâ:µ±ÄãʹÓÃÒ»´ÎÒ»´ÎʹÓÃͬһ¸ö²éѯʱ,µÚÒ»ºÍµÚ¶þ²½ÊÇ×îºÄ·Ñʱ¼ä.
ÕâÀïÒªÉÙʹÓÃÓÃÀ´CompiledQueryµÄÔÚSystem.Data.Linq ÃüÃû¿Õ¼äÖеÄÀà,ʹÓÃCompiledQuery,ÄãÒ»´Î±àÒëÄãµÄ²éѯ²¢ÇÒ´æ´¢ËüÔÚÖ®ºóʹÓõĵط
Ïà¹ØÎĵµ£º
SQL ×¢Èë¹¥»÷ÔÀí¼°·À»¤
ÔÚÈ·ÈÏ¿ÉÒÔ×¢ÈëµÄÇé¿öÏ£¬Ê¹ÓÃÏÂÃæµÄÓï¾ä£º
HTTP://www.163.com/news.asp?id=xx ;and (select count(*) from sysobjects)£¾0
HTTP://www.163.com/news.asp?id=xx ;and (select count(*) from msysobjects)£¾0
Èç¹ûÊý¾Ý¿âÊÇSQLServer£¬ÄÇôµÚÒ»¸öÍøÖ·µÄÒ³ÃæÓëÔÒ³ÃæHTTP://www.163.com/news.asp? ......
1.·ÖÎöϵͳÈÕÖ¾£¬Ò»ÐÐÒ»Ðп´×ÅʵÔÚÀÛÈË£¬ºÜ¶àÎÞ¹ØÈÕÖ¾£¬ÄܹýÂËϾͺÃÁË¡£Îı¾¹¤¾ß¹¦ÄÜʵÔÚÓÐÏÞ£¬ÓÚÊǺõ£¬½«windowsϵͳÈÕÖ¾µ¼Èësql serverÖУ¬Ö±½ÓsqlÖ®……¡£ÊµÑéÁ˼¸°Ñ£¬×îºÃµÄÊÇ£¬Ö±½Ó½«ÏµÍ³ÈÕÖ¾µ¼³öΪÎı¾µÄ£¬Ö±½Ótab·Ö¸îµÄ£¬µ¼ÈësqlµÄʱºò£º
н¨Êý¾Ý¿â->µ¼ÈëÊý¾Ý->ÏÂÒ»²½->Êý¾ÝÔ´Ñ¡ÔñÎı¾Îļþ- ......
Microsoft SQL Server2005¼¼ÊõÄÚÄ»:T-SQL³ÌÐòÉè¼Æ
http://www.amazon.cn/mn/detailApp/ref=sr_1_25?_encoding=UTF8&s=books&qid=1261538877&asin=B0011C2568&sr=8-25
Microsoft SQL Server 2005¼¼ÊõÄÚÄ»£º²éѯ¡¢µ÷ÕûºÍÓÅ»¯
http://www.amazon.cn/mn/detailApp/ref=sr_1_4?_encoding=UTF8&s=books&am ......
ÏÂÁÐÓï¾ä²¿·ÖÊÇMssqlÓï¾ä£¬²»¿ÉÒÔÔÚaccessÖÐʹÓá£
SQL·ÖÀࣺ
DDL—Êý¾Ý¶¨ÒåÓïÑÔ(CREATE£¬ALTER£¬DROP£¬DECLARE)
DML—Êý¾Ý²Ù×ÝÓïÑÔ(SELECT£¬DELETE£¬UPDATE£¬INSERT)
DCL—Êý¾Ý¿ØÖÆÓïÑÔ(GRANT£¬REVOKE£¬COMMIT£¬ROLLBACK)
Ê×ÏÈ,¼òÒª½éÉÜ»ù´¡Óï¾ä£º
1¡¢ËµÃ÷£º´´½¨Êý¾Ý¿â
CREATE DATABASE data ......
Ëæ×ÅB/SģʽӦÓÿª·¢µÄ·¢Õ¹£¬Ê¹ÓÃÕâÖÖģʽ±àдӦÓóÌÐòµÄ³ÌÐòÔ±Ò²Ô½À´Ô½¶à¡£µ«ÊÇÓÉÓÚ³ÌÐòÔ±µÄˮƽ¼°¾ÑéÒ²²Î²î²»Æ룬Ï൱´óÒ»²¿·Ö³ÌÐòÔ±ÔÚ±àд´úÂëµÄʱºò£¬Ã»ÓжÔÓû§ÊäÈëÊý¾ÝµÄºÏ·¨ÐÔ½øÐÐÅжϣ¬Ê¹Ó¦ÓóÌÐò´æÔÚ°²È«Òþ»¼¡£Óû§¿ÉÒÔÌá½»Ò»¶ÎÊý¾Ý¿â²éѯ´úÂ룬¸ù
¾Ý³ÌÐò·µ»ØµÄ½á¹û£¬»ñµÃijЩËûÏëµÃÖªµÄÊý¾Ý£¬Õâ¾ÍÊÇËùνµÄSQL ......