我目前正面临一个性能(它可能会导致以后的缩放问题)问题。我正在处理的应用程序非常复杂,它在 SQL Server 2005 上运行。我需要连接 6-7 个表才能获得所需的数据。到目前为止,每个表包含超过 100,000 行数据。数据库架构不能更改(必须保持原样)。所以我只能尽量优化。我想到了两件事:
尽量不要加入数据库,让应用服务器使用 LINQ 进行过滤:
应用服务器保持原样并尽可能优化 SQL 查询(更多索引、频繁重建索引等):
基本上缓存目前不是我的解决方案(硬件问题,托管问题等),这就是我最初没有提出它的原因。但我确实知道缓存会给我带来什么好处,并且已经多次使用它。
如何在LINQ中执行以下SQL?我实际上正在使用LINQ到NHibernate(但是由于我认为嵌入式lambda表达式,它可能在NHibernate LINQ中不可能).但我想知道如何在LINQ中做到这一点.我以前从未遇到过这种情况.
SELECT c.CustomerID, c.CustomerName --etc
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
WHERE o.Status = 1
public class Customer
{
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public IList<Order> Orders { get; set; }
}
public class Order
{
public int OrderID { get; set; }
public int CustomerID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想做类似的事情:
var customers =
(from c in Customers
where c.Orders.Where(o => o.Status == 1) …Run Code Online (Sandbox Code Playgroud) 在C#中,在以下情况下执行多线程或异步任务的最佳方法是什么?
简化情况:
http请求需要进行5次或更多次Web服务调用.完成后,每个Web服务调用将接收并返回字符串列表作为结果.调用者(5个Web服务调用)需要将5个结果合并为单个字符串列表并将其返回给http调用者.
因为每个线程都需要在最后返回一个值,所以我想知道是否可以使用异步委托.因为我在这方面不是很有经验所以我问这个问题和/或建议.
谢谢!
我问了一个关于构建自定义线程安全通用列表的问题,我现在正试图对它进行单元测试,我完全不知道该怎么做.由于锁定发生在ThreadSafeList类中,我不确定如何在我尝试模仿多个add调用时使列表锁定一段时间.谢谢.
Can_add_one_item_at_a_time
[Test]
public void Can_add_one_item_at_a_time() //this test won't pass
{
//I am not sure how to do this test...
var list = new ThreadSafeList<string>();
//some how need to call lock and sleep inside list instance
//say somehow list locks for 1 sec
var ta = new Thread(x => list.Add("a"));
ta.Start(); //does it need to aboard say before 1 sec if locked
var tb = new Thread(x => list.Add("b"));
tb.Start(); //does it need to aboard say before 1 …Run Code Online (Sandbox Code Playgroud) 我需要访问请求上下文,特别是我的自定义类中的 Items,我不想让它从ServiceStackService继承或在我的 Service 中设置它。
因此,如果我有一个类似下面的类,实现者类 ( ContextItemsGetter) 也实现了IRequiresRequest,我希望该Request属性被填充。
public interface IGetContextItems
{
string Get(string key);
}
public class ContextItemsGetter : IGetContextItems, IRequiresRequest
{
public string Get(string key)
{
//someway to access http context items
//im RequestContext.Instance.Items[key] e.g. Prop1 Prop2
//or Request.blah but Request is always null
}
public IRequest Request { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是,当从真正的HTTP 请求或redis 消息请求调用 SessionIdGetter 时,请求始终为空。难道我做错了什么?目的是解耦,使用Items在http请求和redis消息请求之间传递信息。
我还尝试使用 RequestContext.Instance.Items,这适用于 HTTP 请求,但在 redis 消息请求期间,项目不存在,我在调用 ExecuteMessage 之前填充的键不存在。 …
我正在用NHibernate 2.1替换旧的DAL。我的NHibernate配置可以在我的本地开发机器上运行,但不能在UAT上运行。UAT数据库是在无默认端口上的群集设置。我正在使用类似于以下内容的标准NHibernate confie文件:
<?xml version="1.0" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=(local);Initial Catalog=dbname;User Id=user;Password=********</property>
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
我认为问题是我在NHibernate配置文件中指定连接字符串的方式,因为我现有的DAL可以使用以下连接字符串:
Data Source=uatserver\db01,1433;
Initial Catalog=dbname;
User ID=dbuser;
Password=userpassword
Run Code Online (Sandbox Code Playgroud)
在NHibernate配置文件中,我尝试了以下组合,但没有任何作用,并且收到了不同的错误消息,但大多数情况下,他们说无法连接。
<property name="connection.connection_string">
Server=tcp:(uatserver\db01),1433;
Initial Catalog=dbname;
User ID=dbuser;
Password=userpassword</property>
Run Code Online (Sandbox Code Playgroud)
错误:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。(提供者:TCP提供程序,错误:0-未知此类主机。)
<property name="connection.connection_string">
Server=(uatserver\db01),1433;
Initial Catalog=dbname;
User ID=dbuser;
Password=userpassword</property>
<property name="connection.connection_string">
Server=uatserver\db01,1433;
Initial Catalog=dbname;
User ID=dbuser;
Password=userpassword</property>
<property name="connection.connection_string">
Server=(uatserver\db01, 1433);
Initial Catalog=dbname;
User ID=dbuser;
Password=userpassword</property>
Run Code Online (Sandbox Code Playgroud)
这是日志的最后一行:
[27 Jul 2009 18:27] NHibernate.Connection.DriverConnectionProvider
[DEBUG] Obtaining IDbConnection from Driver
Run Code Online (Sandbox Code Playgroud) 我在一个宠物项目上试验Dapper.我使用SQLite运行所有测试和MySql进行"生产".但是我不确定如何最好地使用Dapper来处理数据库不可知的情况.
我遇到的特殊问题是MySql 不支持主键的Guid类型,因此我使用varchar(40)作为类型(SQLite支持唯一标识符,这是一个guid).所以如果我有一个通用的存储库,问题就出现了,我在尝试从MySql数据库中选择时会遇到麻烦.因为Id属性的类型是Guid,Dapper将抛出" Error parsing column 10 ",因为varchar类型与guid类型不匹配.
如果我将Id属性从guid更改为int,那么GetById中的原始sql会更加棘手,我不知道如何编写.它将类似于:1.启动事务,2.插入,3.选择最后插入的id并返回它.所以如果数据库类型是mysql,那么我将继续使用,然后使用last_insert_id,或者如果它是sqlite,那么使用last_insert_rowid?因为原始sql语法在数据库与数据库之间会有很大差异......
public IEnumerable<T> GetById(Guid id) //convention: Id is always of type Guid.
{
return UnitOfWork.DbConnection.Query<T>(
string.Format(
"select * from {0} where Id = @Id", typeof (T).Name), new {Id = id});
}
Run Code Online (Sandbox Code Playgroud)
其他示例是限制返回的行数(特别是对于分页)等等.那我怎么用dapper编写数据库无关的原始sql查询?也许在我的情况下Dapper不合适?也许我应该在这里使用相同的旧NHibernate.有什么建议?我做错了吗?谢谢!
我的表结构如下所示:
table Tenant: Id[PK], etc
table Contact: Id[PK], FirstName, LastName etc
table Sale: Id[PK], TenantId[FK], SellerId[FK], BuyerId[FK], etc
SellerId is a FK to Contact.Id
BuyerId is a FK to Contact.Id
TenantId is a FK to Tenant.Id
Run Code Online (Sandbox Code Playgroud)
我想使用OrmLite生成类似于以下内容的SQL:
select sale.*
, buyer.FirstName 'BuyerFirstName'
, buyer.LastName 'BuyerLastName'
, seller.FirstName 'SellerFirstName'
, seller.LastName 'SellerLastName'
from sale
left join
contact seller
on sale.SellerId = seller.Id
left join
contact buyer
on sale.BuyerId = buyer.Id
where tenantId = 'guid' -- this is being filtered at a …Run Code Online (Sandbox Code Playgroud) 我想知道如何以集合格式获取帖子数据?假如我有一个表单和一个名为firstname的文本框,通常我输入
var fn = txtFirstName.Text;
Run Code Online (Sandbox Code Playgroud)
从该文本框中获取数据.但我想知道当我点击提交数据发布表格如何获得原始发布数据的集合?
谢谢.
我想获取帖子数据而不是获取.
我有以下JavaScript函数:
function Console() {
this.Log = function(msg) {
if (document.getElementById("console")) {
var console = document.getElementById("console");
console.innerHTML += msg + "<br/>";
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题1: 为什么我需要使用新的关键字?
new Console().Log("hello world");
Run Code Online (Sandbox Code Playgroud)
为什么我不能这样做?
Console().Log("hello world without using new");
Run Code Online (Sandbox Code Playgroud)
问题2:
var logger = function() {
this.log = function(msg) {
new Console().Log(msg);
new Console().Log("log initialized");
}
this.log2 = function(msg) {
new Console().Log(msg);
new Console().Log("log2 initialized");
}
}(); //notice the brackets
Run Code Online (Sandbox Code Playgroud)
由于记录器末尾的(),这不会运行.
new logger().log("hello world");
Run Code Online (Sandbox Code Playgroud)
我知道跟尾()它意味着函数被立即调用,但为什么它不起作用?是因为function(){}(); 不能分配给其他变量?
c# ×4
nhibernate ×2
servicestack ×2
anonymous ×1
asp.net ×1
asynchronous ×1
dapper ×1
database ×1
delegates ×1
forms ×1
function ×1
generic-list ×1
httpcontext ×1
javascript ×1
join ×1
left-join ×1
linq ×1
mysql ×1
orm ×1
performance ×1
post ×1
scaling ×1
sqlite ×1
unit-testing ×1