小编Ale*_*cob的帖子

在我的查询中使用LIMIT时为什么MySQL会变慢?

我试图弄清楚为什么我的查询之一变慢,我怎么能修复它但我对结果感到有些困惑.

我有一个orders包含大约80列和775179行的表,我正在执行以下请求:

SELECT * FROM orders WHERE id_state = 2 AND id_mp IS NOT NULL ORDER BY creation_date DESC LIMIT 200

它在4.5s内返回38行

删除后,ORDER BY我得到了一个很好的改进:

SELECT * FROM orders WHERE id_state = 2 AND id_mp IS NOT NULL LIMIT 200

在0.30秒内有38行

但是当移除而LIMIT没有接触到ORDER BY我得到更好的结果时:

SELECT * FROM orders WHERE id_state = 2 AND id_mp IS NOT NULL ORDER BY creation_date DESC

0.10s中的38行(??)

为什么我的LIMIT如此饥饿?

继续前进

我在发送我的答案之前尝试了一些事情,并在注意到我有一个索引creation_date(这是一个datetime)后我删除了它,第一个查询现在运行在0.10秒.这是为什么 ?

编辑

很好的猜测,我在其他列的索引部分的位置.

mysql> explain …
Run Code Online (Sandbox Code Playgroud)

mysql sql performance

11
推荐指数
3
解决办法
3759
查看次数

使用Spring从外部jar自动装配类

我正在尝试使用Spring构建一个独立的应用程序(不在应用程序服务器中运行),我面临以下问题:

我的独立应用程序(启用弹簧)取决于另一个项目(捆绑为jar),其中包含许多服务com.application.service(带注释@Service).

外部项目中没有与spring相关的配置,独立应用程序上下文非常简单,它只包含:

<context:component-scan base-package="com.application" />

以下是依赖于无法获取的服务的Class示例:

@Service
public class StandaloneService {

    @Autowired
    private SomeService someService;

    // ...
}
Run Code Online (Sandbox Code Playgroud)

StandaloneServiceSomeService在外部jar 中,它包含在独立应用程序中.

错误 :

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.application.SomeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

以下是我创建ApplicationContext并尝试获取服务的方式:

public static void main(String[] args) {

    AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
    BeanFactory factory = (BeanFactory) context; …
Run Code Online (Sandbox Code Playgroud)

spring classpath classloader maven

8
推荐指数
1
解决办法
1万
查看次数

如何使用UUID维护插入顺序

在一个宠物项目中,我开始使用UUID。该应用程序非常简单,它使用binary(16)主键将数据添加到MySQL数据库中。

为了生成PK,我以这种方式使用JUG

UUID uuid = Generators.timeBasedGenerator().generate();

然后我将其转换为字节数组:

byte[] b = UUIDUtil.asByteArray(uuid);

现在的问题是,我对行的插入顺序一无所知。如果我按ID对行进行排序,则一些较新的行将排在较旧的行之前(根据DATETIME字段)

我应该怎么做才能保持行的插入顺序(出于排序目的)?

问题的说明,UUID按ASC排序,我希望created_at处于相同顺序。

select hex(id), created_at from myTable order by id

+----------------------------------+---------------------+
| hex(id)                          | created_at          |
+----------------------------------+---------------------+
| 0913FF1FC53911E39D8D6F7C246EE143 | 2014-04-16 09:30:50 |
| 09378CB1C53911E39D8DD94CAEA8D23F | 2014-04-16 09:30:50 |
| 094A9F83C53911E39D8DDF087D0ED31A | 2014-04-16 09:30:51 |
| 0CBE40D5C0B711E38172B9CB0C485EE3 | 2014-04-10 15:50:17 |
| 0CBF5247C0B711E3817277CAF5E1D5B5 | 2014-04-10 15:50:17 |
| 0CC03CA9C0B711E381721BA12057F9E2 | 2014-04-10 15:50:17 |
| 0CC14E1BC0B711E381720505D5FFDCD3 | 2014-04-10 15:50:17 |
| 0CC2387DC0B711E38172F9A6B29EB613 | …
Run Code Online (Sandbox Code Playgroud)

java mysql uuid

5
推荐指数
2
解决办法
6681
查看次数

没有web.xml log4j配置的Spring应用程序

我有一个Spring + Jersey Web应用程序,我正在从web.xml迁移到注释基本配置.我正在实现WebApplicationInitializer,除了log4j之外一切正常,因为我有一个自定义文件名.

在我的web.xml中

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>/WEB-INF/custom-name-log4j.xml</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

这很有效.

现在,我尝试在Java中做同样的事情:

container.setInitParameter("log4jConfiguration", "/WEB-INF/custom-name-log4j.xml");
Run Code Online (Sandbox Code Playgroud)

这不起作用...我在Tomcat 7.0.62中收到以下错误:

ERROR StatusLogger No Log4j context configuration provided. This is very unusual.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Run Code Online (Sandbox Code Playgroud)

我的WAR文件包含WEB-INF /文件夹中的xml.

除了指定log4jConfiguration参数之外,我还需要做些什么吗?

稍后编辑:WebApplicationInitializer

    @Override
    public void onStartup(ServletContext container) {
        AnnotationConfigWebApplicationContext rootContext =
                new AnnotationConfigWebApplicationContext();
        rootContext.register(MyApplicationConfiguration.class);

        container.setInitParameter("log4jConfiguration", "/WEB-INF/custom-name-log4j.xml");

        rootContext.setConfigLocation("my.package.spring");

        final FilterRegistration.Dynamic characterEncodingFilter = container.addFilter("characterEncodingFilter", new CharacterEncodingFilter());
        characterEncodingFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
        characterEncodingFilter.setInitParameter("encoding", "UTF-8");
        characterEncodingFilter.setInitParameter("forceEncoding", "true");

        container.setInitParameter("spring.profiles.default", "prod");

        rootContext.register(SecurityContextFilter.class);

        container.addListener(new ContextLoaderListener(rootContext)); …
Run Code Online (Sandbox Code Playgroud)

spring log4j jersey log4j2

2
推荐指数
1
解决办法
5512
查看次数

了解嵌套的Spring @Transactional如何工作

我正在将一个EJB应用程序移植到Spring,而我正面临一些问题.该应用程序使用eclipselink独立运行(这就是我们选择spring的原因).

在这个应用程序中,我需要创建一个Order,我首先需要创建一个Customer,OrderLines,然后为这个Order添加一个Payment.

问题是我想在单个事务中进行所有插入操作,这样如果付款未能保留,则不能保留任何内容.我试图实现这一点,但看起来我正在跨越多个独立事务,因为如果发生故障,数据会持久存储到数据库(例如:付款失败,无论如何都会创建客户).

这是切入点:

public static void main(String[] args) {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
    BeanFactory beanFactory = (BeanFactory) context;
    MyService service = beanFactory.getBean(MyService.class);
    service.getNewOrders(true);
}
Run Code Online (Sandbox Code Playgroud)

这是我正在解决的bean(使用beanFactory.getBean):

@Component
@Scope("prototype")
public class MyService {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private PaymentService paymentService;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public void getNewOrders(boolean formattedOutput) {
        try {
            List<RawData> rawData = // Acquire data from a remote web service (http rest based)

            for (RawData …
Run Code Online (Sandbox Code Playgroud)

spring jpa transactional

1
推荐指数
1
解决办法
2435
查看次数

标签 统计

spring ×3

mysql ×2

classloader ×1

classpath ×1

java ×1

jersey ×1

jpa ×1

log4j ×1

log4j2 ×1

maven ×1

performance ×1

sql ×1

transactional ×1

uuid ×1